Changing the value of a preprocessor symbol for testing

I am writing a dynamically growing string buffer. I have the following file .c.

#ifndef STRBUF_GROWTH_SIZE
#define STRBUF_GROWTH_SIZE 4096
#endif

My code uses this constant to redistribute the buffer. Now in the tests I need to set this value to small so that I can check the redistribution. I tried to define this in tests.cpp(all tests are in C ++ using UnitTest ++).

#define STRBUF_GROWTH_SIZE 10
TEST(StringBuffer)
{
    struct strbuf *string = strbuf_init();

    strbuf_add(string, "first");
    CHECK_EQUAL("first", string->buffer);
    CHECK_EQUAL(5, string->length);
    CHECK_EQUAL(10, string->allocated);   /* this fails */

    strbuf_add(string, " second");
    CHECK_EQUAL("first second", string->buffer);
    CHECK_EQUAL(12, string->length);
    CHECK_EQUAL(20, string->allocated); /* this fails */

    strbuf_destroy(string);
}

I am wondering why the value has not changed to 10? How can I solve this problem?

Any thoughts?

+3
source share
2 answers

- (, , ). A #define .

#define , , (, strbuf_init(int growth_size).

, 4096? .

+3

#define .c . . gcc - D. msvc cl /D .

+4

Source: https://habr.com/ru/post/1764735/


All Articles