如何遵循TDD在此malloc包装器上测试失败? [英] How to follow TDD for testing failure on this malloc wrapper?

查看:105
本文介绍了如何遵循TDD在此malloc包装器上测试失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试TDD和C.我想按照TDD方法编写一个简单的malloc包装器.我正在尝试遵循鲍勃·马丁的TDD三定律

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. 除非要通过失败的单元测试,否则不要编写生产代码.
  2. 编写的单元测试不要多于失败,而构建失败就是失败.
  3. 编写的生产代码不要超过通过一项失败的单元测试所需的数量.

这是到目前为止的代码:

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;
    }

这些是我的测试(我正在使用Unity测试框架):

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);
    }

现在如何为失败的malloc编写测试?我知道我可以链接不同版本的malloc,也可以编写一个宏使malloc失败,但这仅在此测试中正确,而在其他情况下,我想使用真正的malloc.

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.

有什么主意吗? 谢谢

推荐答案

要替换malloc(),您可以使用如下函数指针:

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

void* (*pMalloc)(size_t size)

然后使用pMalloc(some size)代替malloc(some size).

如果将指针设置为指向malloc(),如pMalloc = &malloc;所示,则将使用实数malloc().如果将其设置为指向您自己的功能,则将使用您自己的功能.

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.

在函数中,您可以通过返回NULL来模拟malloc()失败.如果测试需要,您也可以从中调用malloc().

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.

类似地,您可以替换您自己的free().

Similarly you can substitute your own free().

在这些替代函数中,您可以记录正在发生的事情.

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

例如,如果将LONG_MAX传递给包装函数(我假设它仍将大小作为long参数),则伪造的malloc()可以记录其实际收到的大小.然后,在测试中,您可以注意到LONG_MAX已从包装器传播为(size_t)LONG_MAXmalloc(),并且如果size_t短于long(就位数而言),则可以检测到差异通过比较值(LONG_MAX != (size_t)LONG_MAX).

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).

您还可以记录伪造的malloc()返回的指针值(从其内部),并将其与包装器返回的值进行比较,以查看它们是否不同.

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.

您可以进一步详细说明这个想法,以进行更多的测试(例如,包装器返回一个非NULL值,但是完全不调用伪造的malloc()(因此,真正的那个)).

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).

在执行所有这些操作时,您不必登录到文件或stdout.您可以为此目的指定一个数据结构,然后对其进行检查.

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.

这篇关于如何遵循TDD在此malloc包装器上测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆