You are not logged in.

#1 2016-01-17 00:39:07

zerophase
Member
Registered: 2015-09-03
Posts: 228

How to unit test memory management code?

I'm developing a memory manager as practice for understanding how something, like malloc and the like work. It's going to be customized for the particular use case, without having deal with inefficiencies. The one problem I've run into is it can be very hard to track bugs down, unless I go slow during development confirming each change works correctly with all of the intended behavior. So, my solution is use unit tests.  Is the right direction to go with the unit tests checking the headers for the correct value, as opposed to checking for memory addresses?

Offline

#2 2016-01-17 00:46:05

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: How to unit test memory management code?

Allow me to ask.  Is this a homework assignment?  If it is, fine.  If not, fine.  But please be frank.
If it is homework, the answers you receive may be a bit more Socratic in nature.  If not, why ? Are you planning on doing an embedded project?


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2016-01-17 01:01:30

zerophase
Member
Registered: 2015-09-03
Posts: 228

Re: How to unit test memory management code?

No, it's not a homework assignment.  I've always wanted to build an open source game engine. As I understand it there isn't an open source project out there that meets the same quality as something like an Unreal. (I get that Unreal is close to being open source, but there's some license issues that I'm not crazy about)  I kind of want to see if it's possible to build an engine with a package management system (I'll probably just use pacman) that would make the engine easy re-configurable for different design directions. (I.E. different rendering engines for different art styles)

Other than that I'm trying to professionally get into the game industry. The one area that's always tripped me up is writing custom allocators. I figure if I lean how to Unit test a simpler example of an allocation scheme that would be applicable to designing more complicated allocators.

Offline

#4 2016-01-17 01:05:16

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: How to unit test memory management code?

Okay, thanks.  Pardon the noise.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#5 2016-01-17 01:09:41

zerophase
Member
Registered: 2015-09-03
Posts: 228

Re: How to unit test memory management code?

It's no problem. I just want to clarify I'm not asking for help with the actual code in the allocator. I'm just not sure how to approach unit testing an allocator.

Offline

#6 2016-01-17 05:52:55

mpan
Member
Registered: 2012-08-01
Posts: 1,188
Website

Re: How to unit test memory management code?

Is the code object oriented? If no, you have a problem, as you're testing a global state and unit testing is painful. Basically you'll not find any good tools for such scenario and those, which can handle it, feel very unnatural in this type of usage. Even worse, you would actually on the border between unit testng and integration testing.

Obviously you can test functions' contracts, especially for values like allocating 0 bytes, 1 bytes, maximum number of bytes and half of the maxium number of bytes (I assume that the allocator takes bytes as its argument). You can also check module's invariatnts, especially in cases when combinations of the arguments mentioned before are used in tandem for consecutive alloations and then deallocated. Not much more you can do, until you have some special features in your library.

But you can test performance, and this is something worth looking at. Check, for example, memory fragmentation for large number of random allocations and deallocations, with various parameters and scenarios — this is a very grave issue for allocators. How well your library perform if multiple threads work concurrently? Does it handle decently nearly-full scenario (use some fake memory limit to prevent OOM killer from kicking in in Linux!). Are there any differences between platforms? How does swapping affect your implementation? There is quite a number of possibilities to analyze here, and I believe you can learn much about performance by trying to deeply understand results you get ­­— especially by trying to explore the results and search if tweaking some testing parameter doesn't provide more interesting irregularities.


Sometimes I seem a bit harsh — don’t get offended too easily!

Offline

Board footer

Powered by FluxBB