How to follow TDD for testing failure on this malloc wrapper?

穿精又带淫゛_ 提交于 2019-12-12 18:01:07

问题


I'm experimenting with TDD and C. I'd like to write a simple malloc wrapper following a TDD approach. I'm trying to follow Bob Martin's Three laws of TDD

  1. Do not write production code unless it is to make a failing unit test pass.
  2. Do not write more of a unit test than is sufficient to fail, and build failures are failures.
  3. Do not write more production code than is sufficient to pass the one failing unit test.

This is my code up until now:

    J_STATUS MemAlloc(long Size, void **OutPtr) {
        J_STATUS ReturnStatus;
        void *Ptr;

        Ptr = NULL;

        if (Size >= 0) {
            Ptr = malloc(Size);
            *OutPtr = Ptr;
            ReturnStatus = SUCCESS;
            //TODO controllare malloc error
        } else {
            ReturnStatus = ERROR;
        }
        return ReturnStatus;
    }

And these are my tests (I'm using Unity test framework):

    #include "../unity/unity.h"
    #include "../src/jMem.h"
    #include "stdlib.h"

    static int *ptr;
    static J_STATUS Result;
    static long Size;
    static long Count;

    void setUp() {
        ptr = NULL;
        Size = 10;
        Count = 5;
    }

    void tearDown() {
        if (ptr != NULL) {
            free(ptr);
        }
    }

    void test_MemAllocShouldAllocateMemoryAndReturnSuccess(void) {
        Result = MemAlloc(Size, (void **) &ptr);
        TEST_ASSERT_EQUAL(SUCCESS, Result);
        TEST_ASSERT_NOT_NULL(ptr);

    }

    void test_MemAllocShouldReturnErrorIfSizeIsNegative(void) {
        Size = -4;

        Result = MemAlloc(Size, (void **) &ptr);

        TEST_ASSERT_EQUAL(ERROR, Result);
    }

Now how do I write a test for a failing malloc? I know I can link a different version of malloc or I could write a macro to make malloc fail, but this should be true only for this test, while in the other I want to use the real malloc.

Any idea? Thank you


回答1:


To substitute malloc() you can use a function pointer like this:

void* (*pMalloc)(size_t size)

And then you use pMalloc(some size) instead of malloc(some size).

If you set the pointer to point at malloc(), as in pMalloc = &malloc;, you'll be using the real malloc(). If you set it to point at your own function, you'll be using that function of yours.

In your function you can simulate a malloc() failure by returning NULL. You can also call malloc() from it, if so is needed for the test.

Similarly you can substitute your own free().

In these substituted functions you can log what's happening.

For example, if you pass LONG_MAX to your wrapper function (I'm assuming it still takes the size as a long argument), your fake malloc() can log the size it has actually received. In the test you can then note that LONG_MAX has propagated from the wrapper to malloc() as (size_t)LONG_MAX, and if size_t is shorter than long (in terms of the number of bits) you can detect the discrepancy by comparing the values (LONG_MAX != (size_t)LONG_MAX).

You can also log the pointer value that your fake malloc() returns (from inside of itself) and compare it with the value returned by your wrapper and see if these are different.

You can elaborate the idea further to come up with more tests (e.g. the wrapper returns a non-NULL value, but the fake malloc() (and therefore the real one) isn't called at all).

While doing all this you don't have to log to a file or to stdout. You can dedicate a data structure for the purpose and then examine it.




回答2:


Maybe that's an edge case you don't need to test. Just write the code. :-)

On the other hand, since we all like to practice good TDD, another option would be to elevate your malloc call to a function parameter. As a function pointer, you can pass the address of the regular malloc in one test and the address of malloc_that_errors in the error test case.

Note that it dirties up the interface of your wrapper, which I personally don't like. It breaks the abstraction you're going for and I'd hate to pass malloc in everywhere. Given that, I'd go back to not testing it. :-D

Hope that helps!

Brandon




回答3:


Your malloc wrapper isn't very convincing when it uses long rather than size_t for the size argument.

You should be able to make it fail by allocating a Very Large amount of memory, use size_t and find the maximum value. Most real malloc()s won't succeed in allocating that, and so they will fail.

This is, of course, still a rather fake way of getting it to fail, I guess the only proper one is to provide hooks so you can substitute a different malloc().




回答4:


Old question but I feel that, depending on your compiler, there is one answer missing. GNU LD has a --wrap symbol option that lets you override certain functions at link time.

void *
__wrap_malloc (int c)
{
  printf ("malloc called with %ld\n", c);
  return __real_malloc (c);
}

As with this code every call to malloc will be replaced with a call to __wrap_malloc and if you would like to call the real malloc it is available through __real_malloc.

This functionality might be available with other linkers aswell I don't know, but for GCC it's a nice alternative.



来源:https://stackoverflow.com/questions/12510195/how-to-follow-tdd-for-testing-failure-on-this-malloc-wrapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!