@@ -22,7 +22,7 @@ void setUp(void)
22
22
void tearDown (void )
23
23
{
24
24
// Reset logging state
25
- circular_buffer_reset ( & g_log_handle -> buffer );
25
+ LOG_deinit ( g_log_handle );
26
26
}
27
27
28
28
void test_LOG_init (void )
@@ -34,6 +34,41 @@ void test_LOG_init(void)
34
34
TEST_ASSERT_EQUAL (LOG_BUFFER_SIZE , g_log_handle -> buffer .size );
35
35
}
36
36
37
+ void test_LOG_init_already_initialized (void )
38
+ {
39
+ // Arrange - Call init again
40
+ LOG_Handle * handle = LOG_init ();
41
+
42
+ // Assert - Should return the same handle
43
+ TEST_ASSERT_EQUAL_PTR (g_log_handle , handle );
44
+ }
45
+
46
+ void test_LOG_deinit (void )
47
+ {
48
+ // Arrange
49
+ LOG_Handle * handle = g_log_handle ;
50
+
51
+ // Act
52
+ LOG_deinit (handle );
53
+
54
+ // Assert - Check if handle is still valid after deinit
55
+ TEST_ASSERT_NOT_NULL (handle );
56
+ TEST_ASSERT_FALSE (handle -> initialized );
57
+ TEST_ASSERT_EQUAL (0 , circular_buffer_available (& handle -> buffer ));
58
+ }
59
+
60
+ void test_LOG_deinit_invalid_handle (void )
61
+ {
62
+ // Arrange
63
+ LOG_Handle invalid_handle = { .initialized = true };
64
+
65
+ // Act - Attempt to deinitialize an invalid handle
66
+ LOG_deinit (& invalid_handle );
67
+
68
+ // Assert - Should not crash, handle remains unchanged
69
+ TEST_ASSERT_TRUE (invalid_handle .initialized );
70
+ }
71
+
37
72
void test_LOG_write (void )
38
73
{
39
74
// Arrange
@@ -59,43 +94,6 @@ void test_LOG_write(void)
59
94
TEST_ASSERT_EQUAL ('\0' , entry .message [entry .length ]);
60
95
}
61
96
62
- void test_LOG_available (void )
63
- {
64
- // Arrange
65
- char const * test_message = "Test log message" ;
66
-
67
- // Act
68
- int bytes_written = LOG_write (LOG_LEVEL_DEBUG , test_message );
69
- size_t available = LOG_available ();
70
-
71
- // Assert
72
- TEST_ASSERT_GREATER_THAN (0 , bytes_written );
73
- TEST_ASSERT_EQUAL (available , bytes_written );
74
- }
75
-
76
- // Test LOG_task processes at most 8 entries per call
77
- void test_LOG_task_partial_processing (void )
78
- {
79
- // Arrange: Write more than 8 entries
80
- const int total_entries = 12 ;
81
- for (int i = 0 ; i < total_entries ; i ++ ) {
82
- int bytes_written = LOG_write (LOG_LEVEL_INFO , "Test entry %d" , i );
83
- TEST_ASSERT_GREATER_THAN (0 , bytes_written );
84
- }
85
-
86
- // Act: Call service once
87
- LOG_task ();
88
-
89
- // Assert: Only 8 processed, 4 remain
90
- TEST_ASSERT_GREATER_THAN (0 , LOG_available ());
91
-
92
- // Act: Call service again
93
- LOG_task ();
94
-
95
- // Assert: Remaining 4 processed, buffer empty
96
- TEST_ASSERT_EQUAL (0 , LOG_available ());
97
- }
98
-
99
97
void test_LOG_write_multiple_entries (void )
100
98
{
101
99
// Arrange & Act
@@ -166,6 +164,24 @@ void test_LOG_write_long_message(void)
166
164
TEST_ASSERT_EQUAL ('\0' , entry .message [entry .length ]);
167
165
}
168
166
167
+ void test_LOG_write_null_message (void )
168
+ {
169
+ // Act - Attempt to write a null message
170
+ int bytes_written = LOG_write (LOG_LEVEL_INFO , NULL );
171
+
172
+ // Assert - Should return -1 for invalid format string
173
+ TEST_ASSERT_EQUAL (-1 , bytes_written );
174
+ }
175
+
176
+ void test_LOG_write_invalid_level (void )
177
+ {
178
+ // Act - Attempt to write with an invalid log level
179
+ int bytes_written = LOG_write ((LOG_Level )999 , "Invalid level test" );
180
+
181
+ // Assert - Should return -1 for invalid log level
182
+ TEST_ASSERT_EQUAL (-1 , bytes_written );
183
+ }
184
+
169
185
void test_LOG_read_entry_success (void )
170
186
{
171
187
// Arrange - Write a message first
@@ -194,7 +210,7 @@ void test_LOG_read_entry_empty_buffer(void)
194
210
TEST_ASSERT_FALSE (result );
195
211
}
196
212
197
- void test_LOG_LOG_read_entry_null_pointer (void )
213
+ void test_LOG_read_entry_null_pointer (void )
198
214
{
199
215
// Arrange
200
216
int bytes_written = LOG_write (LOG_LEVEL_INFO , "Test message" );
@@ -228,6 +244,54 @@ void test_LOG_buffer_overflow(void)
228
244
TEST_ASSERT_EQUAL_STRING (final_entry_message_in_buffer , entry .message );
229
245
}
230
246
247
+ void test_LOG_available (void )
248
+ {
249
+ // Arrange
250
+ char const * test_message = "Test log message" ;
251
+
252
+ // Act
253
+ int bytes_written = LOG_write (LOG_LEVEL_DEBUG , test_message );
254
+ size_t available = LOG_available ();
255
+
256
+ // Assert
257
+ TEST_ASSERT_GREATER_THAN (0 , bytes_written );
258
+ TEST_ASSERT_EQUAL (available , bytes_written );
259
+ }
260
+
261
+ void test_LOG_available_empty_buffer (void )
262
+ {
263
+ // Act
264
+ size_t available = LOG_available ();
265
+
266
+ // Assert - Should be 0 after setUp
267
+ TEST_ASSERT_EQUAL (0 , available );
268
+ }
269
+
270
+ // Test LOG_task processes fewer entries than available
271
+ void test_LOG_task_partial_processing (void )
272
+ {
273
+ // Arrange: Write several entries
274
+ const int total_entries = 12 ;
275
+ for (int i = 0 ; i < total_entries ; i ++ ) {
276
+ int bytes_written = LOG_write (LOG_LEVEL_INFO , "Test entry %d" , i );
277
+ TEST_ASSERT_GREATER_THAN (0 , bytes_written );
278
+ }
279
+
280
+ // Act: Process fewer entries than available
281
+ int processed = LOG_task (8 );
282
+
283
+ // Assert: Only 8 processed, 4 remain
284
+ TEST_ASSERT_EQUAL (8 , processed );
285
+ TEST_ASSERT_GREATER_THAN (0 , LOG_available ());
286
+
287
+ // Act: Call service again
288
+ processed += LOG_task (8 );
289
+
290
+ // Assert: Remaining 4 processed, buffer empty
291
+ TEST_ASSERT_EQUAL (12 , processed );
292
+ TEST_ASSERT_EQUAL (0 , LOG_available ());
293
+ }
294
+
231
295
void test_LOG_task_multiple_entries (void )
232
296
{
233
297
// Arrange - Write multiple entries
@@ -243,9 +307,20 @@ void test_LOG_task_multiple_entries(void)
243
307
TEST_ASSERT_GREATER_THAN (0 , bytes4 );
244
308
245
309
// Act
246
- LOG_task ();
310
+ int processed = LOG_task (4 );
247
311
248
312
// Assert - All entries should be processed and buffer empty
313
+ TEST_ASSERT_EQUAL (4 , processed );
314
+ TEST_ASSERT_EQUAL (0 , LOG_available ());
315
+ }
316
+
317
+ void test_LOG_task_no_entries (void )
318
+ {
319
+ // Act - Call task with no entries
320
+ int processed = LOG_task (5 );
321
+
322
+ // Assert - Should process 0 entries
323
+ TEST_ASSERT_EQUAL (0 , processed );
249
324
TEST_ASSERT_EQUAL (0 , LOG_available ());
250
325
}
251
326
@@ -465,3 +540,25 @@ void test_LOG_buffer_data_integrity_after_wraparound(void)
465
540
}
466
541
}
467
542
}
543
+
544
+ void test_LOG_consistent_state_on_multiple_initializations (void )
545
+ {
546
+ // Arrange - Write a message before reinitialization
547
+ char const * test_message = "Consistent state test" ;
548
+ int bytes_written = LOG_write (LOG_LEVEL_INFO , test_message );
549
+ TEST_ASSERT_GREATER_THAN (0 , bytes_written );
550
+
551
+ // Act - Reinitialize logging system
552
+ LOG_Handle * handle_reinit = LOG_init ();
553
+
554
+ // Assert - Should return the same handle, state should be consistent
555
+ TEST_ASSERT_EQUAL_PTR (g_log_handle , handle_reinit );
556
+ TEST_ASSERT_TRUE (handle_reinit -> initialized );
557
+ TEST_ASSERT_EQUAL (bytes_written , LOG_available ());
558
+
559
+ // Verify we can still read the previously written message
560
+ LOG_Entry entry ;
561
+ TEST_ASSERT_TRUE (LOG_read_entry (& entry ));
562
+ TEST_ASSERT_EQUAL (LOG_LEVEL_INFO , entry .level );
563
+ TEST_ASSERT_EQUAL_STRING (test_message , entry .message );
564
+ }
0 commit comments