77#include "helpers.h"
88#include <unity.h>
99
10- typedef struct fragment_tree_match_item_t
10+ static size_t tree_count ( udpard_tree_t * const root ) // how many make a forest?
1111{
12- size_t offset ;
13- const char * data ; ///< A null-terminated string; NULL at the end.
14- } fragment_tree_match_item_t ;
12+ size_t count = 0 ;
13+ for (udpard_tree_t * p = cavl2_min (root ); p != NULL ; p = cavl2_next_greater (p )) {
14+ count ++ ;
15+ }
16+ return count ;
17+ }
1518
16- static bool fragment_tree_match ( udpard_fragment_t * const head , const fragment_tree_match_item_t items [] )
19+ static udpard_fragment_t * fragment_at ( udpard_tree_t * const root , uint32_t index )
1720{
18- if (head == NULL ) {
19- return (items [0 ].data == NULL );
21+ for (udpard_fragment_t * it = (udpard_fragment_t * )cavl2_min (root ); it != NULL ;
22+ it = (udpard_fragment_t * )cavl2_next_greater (& it -> index_offset )) {
23+ if (index -- == 0U ) {
24+ return it ;
25+ }
2026 }
21- const bool match = (head -> offset == items [0 ].offset ) &&
22- (strlen ((const char * )head -> view .data ) == strlen (items [0 ].data )) &&
23- (memcmp (head -> view .data , items [0 ].data , head -> view .size ) == 0 );
24- return match && fragment_tree_match ((udpard_fragment_t * )cavl2_next_greater (& head -> index_offset ), & items [1 ]);
27+ return NULL ;
28+ }
29+
30+ /// Allocates the payload on the heap, emulating normal transfer reception.
31+ /// The payload shall not contain NUL characters.
32+ static rx_frame_base_t make_frame_base (const udpard_mem_resource_t mem , const size_t offset , const char * const payload )
33+ {
34+ const size_t size = (payload != NULL ) ? strlen (payload ) : 0U ;
35+ void * data = mem .alloc (mem .user , size );
36+ return (rx_frame_base_t ){ .offset = offset ,
37+ .payload = { .data = data , .size = size },
38+ .origin = { .data = data , .size = size } };
2539}
2640
2741static void test_rx_fragment_tree_update_a (void )
@@ -34,6 +48,45 @@ static void test_rx_fragment_tree_update_a(void)
3448 instrumented_allocator_new (& alloc_payload );
3549 const udpard_mem_resource_t mem_payload = instrumented_allocator_make_resource (& alloc_payload );
3650 const udpard_mem_deleter_t del_payload = instrumented_allocator_make_deleter (& alloc_payload );
51+
52+ // Empty payload test
53+ {
54+ udpard_tree_t * root = NULL ;
55+ size_t cov = 0 ;
56+ rx_fragment_tree_update_result_t res = rx_fragment_tree_not_done ;
57+ //
58+ res = rx_fragment_tree_update (& root , //
59+ mem_frag ,
60+ del_payload ,
61+ make_frame_base (mem_payload , 0 , "" ),
62+ 0 ,
63+ 0 ,
64+ & cov );
65+ TEST_ASSERT_EQUAL (rx_fragment_tree_done , res );
66+ TEST_ASSERT_EQUAL_size_t (0 , cov );
67+ TEST_ASSERT_NOT_NULL (root );
68+ TEST_ASSERT_EQUAL (1 , tree_count (root ));
69+ // Check the retained payload.
70+ TEST_ASSERT_EQUAL_size_t (0 , fragment_at (root , 0 )-> offset );
71+ TEST_ASSERT_EQUAL_size_t (0 , fragment_at (root , 0 )-> view .size );
72+ TEST_ASSERT_NULL (fragment_at (root , 1 ));
73+ // Check the heap.
74+ TEST_ASSERT_EQUAL_size_t (1 , alloc_frag .allocated_fragments );
75+ TEST_ASSERT_EQUAL_size_t (0 , alloc_payload .allocated_fragments ); // bc payload empty
76+ TEST_ASSERT_EQUAL_size_t (1 , alloc_frag .count_alloc );
77+ TEST_ASSERT_EQUAL_size_t (1 , alloc_payload .count_alloc );
78+ TEST_ASSERT_EQUAL_size_t (0 , alloc_frag .count_free );
79+ TEST_ASSERT_EQUAL_size_t (0 , alloc_payload .count_free );
80+ // Free the tree (as in freedom). The free tree is free to manifest its own destiny.
81+ udpard_fragment_free_all ((udpard_fragment_t * )root , mem_frag );
82+ // Check the heap.
83+ TEST_ASSERT_EQUAL_size_t (0 , alloc_frag .allocated_fragments );
84+ TEST_ASSERT_EQUAL_size_t (0 , alloc_payload .allocated_fragments );
85+ TEST_ASSERT_EQUAL_size_t (1 , alloc_frag .count_alloc );
86+ TEST_ASSERT_EQUAL_size_t (1 , alloc_payload .count_alloc );
87+ TEST_ASSERT_EQUAL_size_t (1 , alloc_frag .count_free );
88+ TEST_ASSERT_EQUAL_size_t (0 , alloc_payload .count_free ); // bc payload empty
89+ }
3790}
3891
3992static void test_rx_transfer_id_forward_distance (void )
0 commit comments