| #include "../../unity/unity.h" |
| #include <stdlib.h> |
| #include <string.h> |
| #include <stdio.h> |
|
|
| |
| void setUp(void) { |
| |
| } |
| void tearDown(void) { |
| |
| } |
|
|
| |
| static void init_empty_line(struct line* l) |
| { |
| memset(l, 0, sizeof(*l)); |
| } |
|
|
| |
| static void test_extract_field_basic_append(void) |
| { |
| struct line l; |
| init_empty_line(&l); |
|
|
| char buf[] = "hello"; |
| extract_field(&l, buf, (idx_t)5); |
|
|
| TEST_ASSERT_EQUAL_INT(1, (int)l.nfields); |
| TEST_ASSERT_TRUE(l.nfields_allocated >= 1); |
| TEST_ASSERT_NOT_NULL(l.fields); |
| TEST_ASSERT_EQUAL_PTR(buf, l.fields[0].beg); |
| TEST_ASSERT_EQUAL_INT(5, (int)l.fields[0].len); |
|
|
| |
| freeline(&l); |
| } |
|
|
| |
| static void test_extract_field_multiple_growth_and_preserve(void) |
| { |
| struct line l; |
| init_empty_line(&l); |
|
|
| enum { N = 50 }; |
| char storage[N][16]; |
|
|
| for (int i = 0; i < N; ++i) |
| { |
| |
| int n = snprintf(storage[i], sizeof(storage[i]), "f%02d", i); |
| if (n < 0) n = 0; |
| extract_field(&l, storage[i], (idx_t)n); |
|
|
| |
| TEST_ASSERT_EQUAL_INT(i + 1, (int)l.nfields); |
| TEST_ASSERT_TRUE(l.nfields_allocated >= l.nfields); |
| TEST_ASSERT_EQUAL_PTR(storage[i], l.fields[i].beg); |
| TEST_ASSERT_EQUAL_INT(n, (int)l.fields[i].len); |
|
|
| |
| if (i >= 3) |
| { |
| TEST_ASSERT_EQUAL_PTR(storage[0], l.fields[0].beg); |
| TEST_ASSERT_EQUAL_PTR(storage[1], l.fields[1].beg); |
| TEST_ASSERT_EQUAL_PTR(storage[2], l.fields[2].beg); |
| } |
| } |
|
|
| |
| for (int i = 0; i < N; ++i) |
| { |
| int n = (int)strlen(storage[i]); |
| TEST_ASSERT_EQUAL_PTR(storage[i], l.fields[i].beg); |
| TEST_ASSERT_EQUAL_INT(n, (int)l.fields[i].len); |
| } |
|
|
| |
| freeline(&l); |
| } |
|
|
| |
| static void test_extract_field_no_realloc_when_capacity_available(void) |
| { |
| struct line l; |
| init_empty_line(&l); |
|
|
| |
| l.nfields_allocated = 8; |
| l.fields = (struct field*) malloc(sizeof(struct field) * l.nfields_allocated); |
| TEST_ASSERT_NOT_NULL(l.fields); |
|
|
| struct field* oldptr = l.fields; |
|
|
| char buf1[] = "A"; |
| extract_field(&l, buf1, (idx_t)1); |
|
|
| TEST_ASSERT_EQUAL_PTR(oldptr, l.fields); |
| TEST_ASSERT_EQUAL_INT(1, (int)l.nfields); |
| TEST_ASSERT_EQUAL_PTR(buf1, l.fields[0].beg); |
| TEST_ASSERT_EQUAL_INT(1, (int)l.fields[0].len); |
|
|
| |
| freeline(&l); |
| } |
|
|
| |
| static void test_extract_field_zero_length(void) |
| { |
| struct line l; |
| init_empty_line(&l); |
|
|
| char dummy = 'X'; |
| extract_field(&l, &dummy, (idx_t)0); |
|
|
| TEST_ASSERT_EQUAL_INT(1, (int)l.nfields); |
| TEST_ASSERT_EQUAL_PTR(&dummy, l.fields[0].beg); |
| TEST_ASSERT_EQUAL_INT(0, (int)l.fields[0].len); |
|
|
| |
| char dummy2 = 'Y'; |
| extract_field(&l, &dummy2, (idx_t)0); |
| TEST_ASSERT_EQUAL_INT(2, (int)l.nfields); |
| TEST_ASSERT_EQUAL_PTR(&dummy2, l.fields[1].beg); |
| TEST_ASSERT_EQUAL_INT(0, (int)l.fields[1].len); |
|
|
| |
| freeline(&l); |
| } |
|
|
| |
| static void test_extract_field_large_len_value(void) |
| { |
| struct line l; |
| init_empty_line(&l); |
|
|
| char buf[] = "x"; |
| idx_t large_len = (idx_t)123456; |
| extract_field(&l, buf, large_len); |
|
|
| TEST_ASSERT_EQUAL_INT(1, (int)l.nfields); |
| TEST_ASSERT_EQUAL_PTR(buf, l.fields[0].beg); |
| TEST_ASSERT_EQUAL_INT((int)large_len, (int)l.fields[0].len); |
|
|
| |
| freeline(&l); |
| } |
|
|
| int main(void) |
| { |
| UNITY_BEGIN(); |
| RUN_TEST(test_extract_field_basic_append); |
| RUN_TEST(test_extract_field_multiple_growth_and_preserve); |
| RUN_TEST(test_extract_field_no_realloc_when_capacity_available); |
| RUN_TEST(test_extract_field_zero_length); |
| RUN_TEST(test_extract_field_large_len_value); |
| return UNITY_END(); |
| } |