File size: 3,645 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "../../unity/unity.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void setUp(void) {
  /* Setup code here, or leave empty */
}

void tearDown(void) {
  /* Cleanup code here, or leave empty */
}

/* Helper to allocate and zero a struct line */
static struct line* alloc_zeroed_line(void) {
  struct line* l = (struct line*)malloc(sizeof(struct line));
  TEST_ASSERT_NOT_NULL(l);
  memset(l, 0, sizeof(*l));
  return l;
}

void test_freeline_null_pointer_ok(void) {
  /* Should not crash or modify anything when given NULL */
  freeline(NULL);
  /* If we got here, no crash; record a trivial assertion */
  TEST_ASSERT_TRUE(1);
}

void test_freeline_frees_and_nulls_members(void) {
  struct line* l = alloc_zeroed_line();

  /* Simulate populated fields and buffer */
  l->nfields = 3;
  l->nfields_allocated = 3;
  l->fields = (struct field*)malloc(sizeof(struct field) * 3);
  TEST_ASSERT_NOT_NULL(l->fields);

  /* Field contents don't matter to freeline; avoid extra allocations */
  for (int i = 0; i < 3; i++) {
    l->fields[i].beg = NULL;
    l->fields[i].len = 0;
  }

  l->buf.buffer = (char*)malloc(16);
  TEST_ASSERT_NOT_NULL(l->buf.buffer);
  memset(l->buf.buffer, 'X', 16);
  l->buf.length = 16;

  freeline(l);

  /* After freeline: pointers are NULLed; counters/length unchanged */
  TEST_ASSERT_NULL(l->fields);
  TEST_ASSERT_NULL(l->buf.buffer);
  TEST_ASSERT_EQUAL_INT(3, (int)l->nfields);
  TEST_ASSERT_EQUAL_INT(3, (int)l->nfields_allocated);
  TEST_ASSERT_EQUAL_INT(16, (int)l->buf.length);

  free(l);
}

void test_freeline_idempotent_on_same_line(void) {
  struct line* l = alloc_zeroed_line();

  l->fields = (struct field*)malloc(sizeof(struct field));
  TEST_ASSERT_NOT_NULL(l->fields);
  l->nfields = 1;
  l->nfields_allocated = 1;

  l->buf.buffer = (char*)malloc(8);
  TEST_ASSERT_NOT_NULL(l->buf.buffer);
  memset(l->buf.buffer, 'Y', 8);
  l->buf.length = 8;

  /* First call frees and NULLs pointers */
  freeline(l);
  TEST_ASSERT_NULL(l->fields);
  TEST_ASSERT_NULL(l->buf.buffer);

  /* Second call should be safe (free(NULL)) and remain NULL */
  freeline(l);
  TEST_ASSERT_NULL(l->fields);
  TEST_ASSERT_NULL(l->buf.buffer);

  free(l);
}

void test_freeline_only_fields_allocated(void) {
  struct line* l = alloc_zeroed_line();

  l->fields = (struct field*)malloc(sizeof(struct field) * 2);
  TEST_ASSERT_NOT_NULL(l->fields);
  l->nfields = 2;
  l->nfields_allocated = 2;

  /* Ensure buffer is NULL/non-allocated */
  l->buf.buffer = NULL;
  l->buf.length = 5; /* length should remain unchanged */

  freeline(l);

  TEST_ASSERT_NULL(l->fields);
  TEST_ASSERT_NULL(l->buf.buffer);
  TEST_ASSERT_EQUAL_INT(2, (int)l->nfields);
  TEST_ASSERT_EQUAL_INT(2, (int)l->nfields_allocated);
  TEST_ASSERT_EQUAL_INT(5, (int)l->buf.length);

  free(l);
}

void test_freeline_only_buffer_allocated(void) {
  struct line* l = alloc_zeroed_line();

  l->fields = NULL;
  l->nfields = 0;
  l->nfields_allocated = 0;

  l->buf.buffer = (char*)malloc(32);
  TEST_ASSERT_NOT_NULL(l->buf.buffer);
  memset(l->buf.buffer, 'Z', 32);
  l->buf.length = 32;

  freeline(l);

  TEST_ASSERT_NULL(l->fields);
  TEST_ASSERT_NULL(l->buf.buffer);
  TEST_ASSERT_EQUAL_INT(0, (int)l->nfields);
  TEST_ASSERT_EQUAL_INT(0, (int)l->nfields_allocated);
  TEST_ASSERT_EQUAL_INT(32, (int)l->buf.length);

  free(l);
}

int main(void) {
  UNITY_BEGIN();

  RUN_TEST(test_freeline_null_pointer_ok);
  RUN_TEST(test_freeline_frees_and_nulls_members);
  RUN_TEST(test_freeline_idempotent_on_same_line);
  RUN_TEST(test_freeline_only_fields_allocated);
  RUN_TEST(test_freeline_only_buffer_allocated);

  return UNITY_END();
}