native_part_chunk_test.c (2098B)
1 #include <stdio.h> 2 3 #include "arch/native_target.h" 4 #include "lib/kit_unit.h" 5 6 typedef struct ChunkCase { 7 u32 total; 8 u32 max_chunk; 9 u32 count; 10 u32 offsets[4]; 11 u32 sizes[4]; 12 } ChunkCase; 13 14 static void check_case(KitUnit* unit, const ChunkCase* tc) { 15 NativePartChunkIter it = native_part_chunks(tc->total, tc->max_chunk); 16 u32 offset = 0; 17 u32 size = 0; 18 u32 count = 0; 19 u32 covered = 0; 20 while (native_part_chunk_next(&it, &offset, &size)) { 21 CU_EXPECT(unit, count < tc->count, 22 "total=%u max=%u produced an extra chunk", tc->total, 23 tc->max_chunk); 24 if (count >= tc->count) return; 25 CU_EXPECT(unit, offset == tc->offsets[count], 26 "total=%u max=%u chunk %u offset: got %u expected %u", tc->total, 27 tc->max_chunk, count, offset, tc->offsets[count]); 28 CU_EXPECT(unit, size == tc->sizes[count], 29 "total=%u max=%u chunk %u size: got %u expected %u", tc->total, 30 tc->max_chunk, count, size, tc->sizes[count]); 31 CU_EXPECT(unit, size != 0u && (size & (size - 1u)) == 0u && 32 size <= tc->max_chunk, 33 "total=%u max=%u chunk %u is not a legal native width", 34 tc->total, tc->max_chunk, count); 35 covered += size; 36 count++; 37 } 38 CU_EXPECT(unit, count == tc->count, 39 "total=%u max=%u chunk count: got %u expected %u", tc->total, 40 tc->max_chunk, count, tc->count); 41 CU_EXPECT(unit, covered == tc->total, 42 "total=%u max=%u covered %u bytes", tc->total, tc->max_chunk, 43 covered); 44 } 45 46 int main(void) { 47 static const ChunkCase cases[] = { 48 {1u, 8u, 1u, {0u}, {1u}}, 49 {3u, 8u, 2u, {0u, 2u}, {2u, 1u}}, 50 {6u, 8u, 2u, {0u, 4u}, {4u, 2u}}, 51 {7u, 8u, 3u, {0u, 4u, 6u}, {4u, 2u, 1u}}, 52 {8u, 8u, 1u, {0u}, {8u}}, 53 {12u, 4u, 3u, {0u, 4u, 8u}, {4u, 4u, 4u}}, 54 }; 55 KitUnit unit; 56 kit_unit_init(&unit); 57 for (u32 i = 0; i < (u32)(sizeof cases / sizeof cases[0]); ++i) 58 check_case(&unit, &cases[i]); 59 kit_unit_summary(&unit, "native-part-chunks"); 60 return kit_unit_status(&unit); 61 }