firmware  v0.1.2
Chromation Spectrometer Dev-Kit
test_RecordedArg.c
1 #include "test_RecordedArg.h"
2 #include <RecordedArg.h>
3 #include <unity.h>
4 #include <glib.h> // GString, GList
5 //=====[ List of tests ]=====
6  // [x] SetupRecord_uint8_t defines uint8_t operations for the RecordedArg
7  // [x] SetupRecord_uint8_t points pArg at a uint8_t
8  // [x] Destroy for SetupRecord_uint8_t frees a uint8_t
9  // [x] Print for SetupRecord_uint8_t prints a uint8_t to a string
10  // [x] Match for SetupRecord_uint8_t checks if uint8_t values match
11  // [x] SetupRecord_uint16_t defines uint16_t operations for the RecordedArg
12  // [x] SetupRecord_uint16_t points pArg at a uint16_t
13  // [x] Destroy for SetupRecord_uint16_t frees a uint16_t
14  // [x] Print for SetupRecord_uint16_t prints a uint16_t to a string
15  // [x] Match for SetupRecord_uint16_t checks if uint16_t values match
16  // [x] SetupRecord_GString defines GString operations for the RecordedArg
17  // [x] SetupRecord_GString points pArg at a GString
18  // [x] Destroy for SetupRecord_GString frees a GString
19  // [x] Print for SetupRecord_GString prints a GString to a string
20  // [x] Match for SetupRecord_GString checks if GString values match
21  // [x] SetupRecord_p_uint8_t defines (uint8_t *) operations for the RecordedArg
22  // [x] SetupRecord_p_uint8_t points pArg at a pointer to an uint8_t
23  // [x] Destroy for SetupRecord_p_uint8_t frees a pointer to an uint8_t
24  // [x] Print for SetupRecord_p_uint8_t derefs and prints value to a string
25  // [x] Match for SetupRecord_p_uint8_t derefs and checks if values match
26  // [x] SetupRecord_p_GString defines (GString *) operations for the RecordedArg
27  // [x] SetupRecord_p_GString points pArg at a pointer to a GString
28  // [x] Destroy for SetupRecord_p_GString frees a pointer to a GString
29  // [x] Print for SetupRecord_p_GString derefs and prints value to a string
30  // [x] Match for SetupRecord_p_GString derefs and checks if values match
31 
32 void SetUp_RecordedArg(void){}
33 void TearDown_RecordedArg(void){}
34 void SetupRecord_uint8_t_points_pArg_at_a_uint8_t(void)
35 {
36  //=====[ Setup ]=====
37  uint8_t b1 = 0xAB;
38  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_uint8_t);
39  *((uint8_t *)record_of_arg1->pArg) = b1;
40  //=====[ Operate and Test ]=====
41  TEST_ASSERT_EQUAL_UINT8(b1, *((uint8_t *)record_of_arg1->pArg));
42  //=====[ Teardown ]=====
43  RecordedArg_destroy(record_of_arg1);
44  record_of_arg1 = NULL;
45 }
46 void Destroy_for_SetupRecord_uint8_t_frees_a_uint8_t(void)
47 {
48  //=====[ Setup ]=====
49  uint8_t b1 = 0xAB;
50  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_uint8_t);
51  *((uint8_t *)record_of_arg1->pArg) = b1;
52  //=====[ Lame test: what else can I do? ]=====
53  TEST_ASSERT_NOT_EQUAL(NULL, record_of_arg1->Destroy);
54  //=====[ Teardown ]=====
55  RecordedArg_destroy(record_of_arg1);
56  record_of_arg1 = NULL;
57 }
58 void Print_for_SetupRecord_uint8_t_prints_a_uint8_t_to_a_string(void)
59 {
60  //=====[ Setup ]=====
61  uint8_t b1 = 0x12;
62  GString *expected = g_string_new("(uint8_t)0x12");
63  GString *actual = g_string_new(NULL); // Call Print to write to actual.
64  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_uint8_t);
65  *((uint8_t *)record_of_arg1->pArg) = b1;
66  //=====[ Operate ]=====
67  record_of_arg1->Print(actual, record_of_arg1);
68  //=====[ Test ]=====
69  TEST_ASSERT_EQUAL_STRING( expected->str, actual->str);
70  //=====[ Teardown ]=====
71  g_string_free(expected, true);
72  g_string_free(actual, true);
73  RecordedArg_destroy(record_of_arg1);
74  record_of_arg1 = NULL;
75 }
76 void Match_for_SetupRecord_uint8_t_checks_if_uint8_t_values_match(void)
77 {
78  //=====[ Setup ]=====
79  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_uint8_t);
80  RecordedArg *record_of_arg2 = RecordedArg_new(SetupRecord_uint8_t);
81  RecordedArg *record_of_arg3 = RecordedArg_new(SetupRecord_uint8_t);
82  uint8_t b3 = 0x03; // arg1 and arg2
83  uint8_t b9 = 0x09; // arg3
84  *((uint8_t *)record_of_arg1->pArg) = b9;
85  *((uint8_t *)record_of_arg2->pArg) = b9;
86  *((uint8_t *)record_of_arg3->pArg) = b3;
87  //=====[ Operate and Test ]=====
88  TEST_ASSERT_TRUE (record_of_arg1->Match(record_of_arg1, record_of_arg2));
89  TEST_ASSERT_FALSE(record_of_arg1->Match(record_of_arg1, record_of_arg3));
90  //=====[ Teardown ]=====
91  RecordedArg_destroy(record_of_arg1);
92  record_of_arg1 = NULL;
93  RecordedArg_destroy(record_of_arg2);
94  record_of_arg2 = NULL;
95  RecordedArg_destroy(record_of_arg3);
96  record_of_arg3 = NULL;
97 }
98 
99 void SetupRecord_uint16_t_points_pArg_at_a_uint16_t(void)
100 {
101  //=====[ Setup ]=====
102  uint16_t b1 = 0xAB;
103  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_uint16_t);
104  *((uint16_t *)record_of_arg1->pArg) = b1;
105  //=====[ Operate and Test ]=====
106  TEST_ASSERT_EQUAL_UINT8(b1, *((uint16_t *)record_of_arg1->pArg));
107  //=====[ Teardown ]=====
108  RecordedArg_destroy(record_of_arg1);
109  record_of_arg1 = NULL;
110 }
111 void Destroy_for_SetupRecord_uint16_t_frees_a_uint16_t(void)
112 {
113  //=====[ Setup ]=====
114  uint16_t b1 = 0xAB;
115  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_uint16_t);
116  *((uint16_t *)record_of_arg1->pArg) = b1;
117  //=====[ Lame test: what else can I do? ]=====
118  TEST_ASSERT_NOT_EQUAL(NULL, record_of_arg1->Destroy);
119  //=====[ Teardown ]=====
120  RecordedArg_destroy(record_of_arg1);
121  record_of_arg1 = NULL;
122 }
123 void Print_for_SetupRecord_uint16_t_prints_a_uint16_t_to_a_string(void)
124 {
125  //=====[ Setup ]=====
126  uint16_t b1 = 0x1234;
127  GString *expected = g_string_new("(uint16_t)0x1234");
128  GString *actual = g_string_new(NULL); // Call Print to write to actual.
129  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_uint16_t);
130  *((uint16_t *)record_of_arg1->pArg) = b1;
131  //=====[ Operate ]=====
132  record_of_arg1->Print(actual, record_of_arg1);
133  //=====[ Test ]=====
134  TEST_ASSERT_EQUAL_STRING( expected->str, actual->str);
135  //=====[ Teardown ]=====
136  g_string_free(expected, true);
137  g_string_free(actual, true);
138  RecordedArg_destroy(record_of_arg1);
139  record_of_arg1 = NULL;
140 }
141 void Match_for_SetupRecord_uint16_t_checks_if_uint16_t_values_match(void)
142 {
143  //=====[ Setup ]=====
144  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_uint16_t);
145  RecordedArg *record_of_arg2 = RecordedArg_new(SetupRecord_uint16_t);
146  RecordedArg *record_of_arg3 = RecordedArg_new(SetupRecord_uint16_t);
147  uint16_t b3 = 0x03; // arg1 and arg2
148  uint16_t b9 = 0x09; // arg3
149  *((uint16_t *)record_of_arg1->pArg) = b9;
150  *((uint16_t *)record_of_arg2->pArg) = b9;
151  *((uint16_t *)record_of_arg3->pArg) = b3;
152  //=====[ Operate and Test ]=====
153  TEST_ASSERT_TRUE (record_of_arg1->Match(record_of_arg1, record_of_arg2));
154  TEST_ASSERT_FALSE(record_of_arg1->Match(record_of_arg1, record_of_arg3));
155  //=====[ Teardown ]=====
156  RecordedArg_destroy(record_of_arg1);
157  record_of_arg1 = NULL;
158  RecordedArg_destroy(record_of_arg2);
159  record_of_arg2 = NULL;
160  RecordedArg_destroy(record_of_arg3);
161  record_of_arg3 = NULL;
162 }
163 
164 void SetupRecord_GString_points_pArg_at_a_GString(void)
165 {
166  //=====[ Setup ]=====
167  GString *pGS1 = g_string_new("string 1");
168  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_GString);
169  record_of_arg1->pArg = (void *)g_string_new(pGS1->str);
170  //=====[ Operate and Test ]=====
171  TEST_ASSERT_EQUAL_STRING(pGS1->str, ((GString *)record_of_arg1->pArg)->str);
172  //=====[ Teardown ]=====
173  g_string_free(pGS1, true);
174  RecordedArg_destroy(record_of_arg1);
175  record_of_arg1 = NULL;
176 }
177 void Destroy_for_SetupRecord_GString_frees_a_GString(void)
178 {
179  //=====[ Setup ]=====
180  GString *pGS1 = g_string_new("string 1");
181  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_GString);
182  record_of_arg1->pArg = (void *)g_string_new(pGS1->str);
183  //=====[ Lame test: what else can I do? ]=====
184  TEST_ASSERT_NOT_EQUAL(NULL, record_of_arg1->Destroy);
185  //=====[ Teardown ]=====
186  RecordedArg_destroy(record_of_arg1);
187  record_of_arg1 = NULL;
188 }
189 void Print_for_SetupRecord_GString_prints_a_GString_to_a_string(void)
190 {
191  //=====[ Setup ]=====
192  GString *expected = g_string_new("(GString)bob");
193  GString *actual = g_string_new(NULL); // Call Print to write to actual.
194  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_GString);
195  record_of_arg1->pArg = (void *)g_string_new("bob");
196  //=====[ Operate ]=====
197  record_of_arg1->Print(actual, record_of_arg1);
198  //=====[ Test ]=====
199  TEST_ASSERT_EQUAL_STRING( expected->str, actual->str);
200  //=====[ Teardown ]=====
201  g_string_free(expected, true);
202  g_string_free(actual, true);
203  RecordedArg_destroy(record_of_arg1);
204  record_of_arg1 = NULL;
205 }
206 void Match_for_SetupRecord_GString_checks_if_GString_values_match(void)
207 {
208  //=====[ Setup ]=====
209  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_GString);
210  RecordedArg *record_of_arg2 = RecordedArg_new(SetupRecord_GString);
211  RecordedArg *record_of_arg3 = RecordedArg_new(SetupRecord_GString);
212  GString *str3 = g_string_new("string 3"); // arg1 and arg2
213  GString *str9 = g_string_new("string 9"); // arg3
214  record_of_arg1->pArg = (void *)g_string_new(str9->str);
215  record_of_arg2->pArg = (void *)g_string_new(str9->str);
216  record_of_arg3->pArg = (void *)g_string_new(str3->str);
217  //=====[ Operate and Test ]=====
218  TEST_ASSERT_TRUE (record_of_arg1->Match(record_of_arg1, record_of_arg2));
219  TEST_ASSERT_FALSE(record_of_arg1->Match(record_of_arg1, record_of_arg3));
220  //=====[ Teardown ]=====
221  g_string_free(str3, true);
222  g_string_free(str9, true);
223  RecordedArg_destroy(record_of_arg1);
224  record_of_arg1 = NULL;
225  RecordedArg_destroy(record_of_arg2);
226  record_of_arg2 = NULL;
227  RecordedArg_destroy(record_of_arg3);
228  record_of_arg3 = NULL;
229 }
230 
231 void SetupRecord_p_uint8_t_points_pArg_at_a_pointer_to_an_uint8_t(void)
232 {
233  //=====[ Setup ]=====
234  uint8_t b1 = 0x01;
235  uint8_t *b1_address = &b1;
236  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_p_uint8_t);
237  *((uint8_t **)record_of_arg1->pArg) = b1_address;
238  // FAIL: Expected 71 Was 1 : record_of_arg1->pArg = (void *)b1_address;
239  //=====[ Operate and Test ]=====
240  TEST_ASSERT_EQUAL_PTR(b1_address, *((uint8_t **)record_of_arg1->pArg));
241  //=====[ Teardown ]=====
242  RecordedArg_destroy(record_of_arg1);
243  record_of_arg1 = NULL;
244 }
245 void Destroy_for_SetupRecord_p_uint8_t_frees_a_pointer_to_an_uint8_t(void)
246 {
247  //=====[ Setup ]=====
248  uint8_t b1 = 0x01;
249  uint8_t *b1_address = &b1;
250  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_p_uint8_t);
251  *((uint8_t **)record_of_arg1->pArg) = b1_address;
252  //=====[ Lame test: what else can I do? ]=====
253  TEST_ASSERT_NOT_EQUAL(NULL, record_of_arg1->Destroy);
254  //=====[ Teardown ]=====
255  RecordedArg_destroy(record_of_arg1);
256  record_of_arg1 = NULL;
257 }
258 void Print_for_SetupRecord_p_uint8_t_derefs_and_prints_value_to_a_string(void)
259 {
260  //=====[ Setup ]=====
261  GString *expected = g_string_new("(uint8_t)0x12"); uint8_t b1 = 0x12;
262  uint8_t *b1_address = &b1;
263  GString *actual = g_string_new(NULL); // Call Print to write to actual.
264  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_p_uint8_t);
265  *((uint8_t **)record_of_arg1->pArg) = b1_address;
266  //=====[ Operate ]=====
267  record_of_arg1->Print(actual, record_of_arg1);
268  //=====[ Test ]=====
269  TEST_ASSERT_EQUAL_STRING( expected->str, actual->str);
270  //=====[ Teardown ]=====
271  g_string_free(expected, true);
272  g_string_free(actual, true);
273  RecordedArg_destroy(record_of_arg1);
274  record_of_arg1 = NULL;
275 }
276 void Match_for_SetupRecord_p_uint8_t_derefs_and_checks_if_values_match(void)
277 {
278  //=====[ Setup ]=====
279  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_p_uint8_t);
280  RecordedArg *record_of_arg2 = RecordedArg_new(SetupRecord_p_uint8_t);
281  RecordedArg *record_of_arg3 = RecordedArg_new(SetupRecord_p_uint8_t);
282  uint8_t b3 = 0x03; uint8_t *b3_address = &b3; // arg1 and arg2
283  uint8_t b9 = 0x09; uint8_t *b9_address = &b9; // arg3
284  *((uint8_t **)record_of_arg1->pArg) = b9_address;
285  *((uint8_t **)record_of_arg2->pArg) = b9_address;
286  *((uint8_t **)record_of_arg3->pArg) = b3_address;
287  //=====[ Operate and Test ]=====
288  TEST_ASSERT_TRUE (record_of_arg1->Match(record_of_arg1, record_of_arg2));
289  TEST_ASSERT_FALSE(record_of_arg1->Match(record_of_arg1, record_of_arg3));
290  //=====[ Teardown ]=====
291  RecordedArg_destroy(record_of_arg1);
292  record_of_arg1 = NULL;
293  RecordedArg_destroy(record_of_arg2);
294  record_of_arg2 = NULL;
295  RecordedArg_destroy(record_of_arg3);
296  record_of_arg3 = NULL;
297 }
298 
299 void SetupRecord_p_GString_points_pArg_at_a_pointer_to_a_GString(void)
300 {
301  //=====[ Setup ]=====
302  GString *p_gstring_arg1 = g_string_new("string 1");
303  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_p_GString);
304  *((GString **)record_of_arg1->pArg) = p_gstring_arg1;
305  //=====[ Operate and Test ]=====
306  TEST_ASSERT_EQUAL_STRING(
307  p_gstring_arg1->str,
308  (*((GString **)record_of_arg1->pArg))->str
309  );
310  //=====[ Teardown ]=====
311  g_string_free(p_gstring_arg1, true);
312  RecordedArg_destroy(record_of_arg1);
313  record_of_arg1 = NULL;
314 }
315 void Destroy_for_SetupRecord_p_GString_frees_a_pointer_to_a_GString(void)
316 {
317  //=====[ Setup ]=====
318  GString *p_gstring_arg1 = g_string_new("string 1");
319  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_p_GString);
320  *((GString **)record_of_arg1->pArg) = p_gstring_arg1;
321  //=====[ Lame test: what else can I do? ]=====
322  TEST_ASSERT_NOT_EQUAL(NULL, record_of_arg1->Destroy);
323  //=====[ Teardown ]=====
324  RecordedArg_destroy(record_of_arg1);
325  record_of_arg1 = NULL;
326 }
327 void Print_for_SetupRecord_p_GString_derefs_and_prints_value_to_a_string(void)
328 {
329  //=====[ Setup ]=====
330  GString *expected = g_string_new("(GString *)bob");
331  GString *actual = g_string_new(NULL); // Call Print to write to actual.
332  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_p_GString);
333  GString *p_gstring_arg1 = g_string_new("bob");
334  *((GString **)record_of_arg1->pArg) = p_gstring_arg1;
335  //=====[ Operate ]=====
336  record_of_arg1->Print(actual, record_of_arg1);
337  //=====[ Test ]=====
338  TEST_ASSERT_EQUAL_STRING( expected->str, actual->str);
339  //=====[ Teardown ]=====
340  g_string_free(expected, true);
341  g_string_free(actual, true);
342  RecordedArg_destroy(record_of_arg1);
343  record_of_arg1 = NULL;
344 }
345 void Match_for_SetupRecord_p_GString_derefs_and_checks_if_values_match(void)
346 {
347  //=====[ Setup ]=====
348  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_p_GString);
349  RecordedArg *record_of_arg2 = RecordedArg_new(SetupRecord_p_GString);
350  RecordedArg *record_of_arg3 = RecordedArg_new(SetupRecord_p_GString);
351  GString *str9 = g_string_new("string 9"); // arg1 and arg2
352  GString *str3 = g_string_new("string 3"); // arg3
353  *((GString **)record_of_arg1->pArg) = str9;
354  *((GString **)record_of_arg2->pArg) = str9;
355  *((GString **)record_of_arg3->pArg) = str3;
356  //=====[ Operate and Test ]=====
357  TEST_ASSERT_TRUE (record_of_arg1->Match(record_of_arg1, record_of_arg2));
358  TEST_ASSERT_FALSE(record_of_arg1->Match(record_of_arg1, record_of_arg3));
359  //=====[ Teardown ]=====
360  g_string_free(str3, true);
361  g_string_free(str9, true);
362  RecordedArg_destroy(record_of_arg1);
363  record_of_arg1 = NULL;
364  RecordedArg_destroy(record_of_arg2);
365  record_of_arg2 = NULL;
366  RecordedArg_destroy(record_of_arg3);
367  record_of_arg3 = NULL;
368 }