client/tests/inflate_fast.c (4128B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_250C1F70(void); 5 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 6 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 7 recomp_func_t recomp_lookup(uint32_t address) { abort(); } 8 static unsigned yields; 9 static void observe_yield(void) { ++yields; } 10 11 int main(void) 12 { 13 assert(guest_map("inflate fixture", 0x50000000, 0x50000)); 14 assert(guest_map("inflate masks", 0x251b0000, 0x10000)); 15 for (unsigned i = 0; i <= 16; ++i) MEM32(0x251b7ff8 + i * 4) = (1u << i) - 1; 16 const struct { 17 unsigned literal_operation, length, distance_operation, distance; 18 unsigned initial_bits, output_bytes, final_bits, result, message, edges; 19 } cases[] = { 20 {0, 65, 80, 1, 0, 1, 7, 0, 0, 3}, 21 {96, 0, 80, 1, 0, 0, 7, 1, 0, 3}, 22 {64, 0, 80, 1, 0, 0, 7, 0xfffffffd, 0x251b6de0, 3}, 23 {80, 258, 80, 1, 0, 258, 6, 0, 0, 259}, 24 {80, 5, 80, 3, 0, 5, 6, 0, 0, 8}, 25 {0, 65, 80, 1, 31, 1, 30, 0, 0, 0}, 26 {80, 3, 64, 1, 0, 0, 6, 0xfffffffd, 0x251b6dc8, 3}, 27 }; 28 for (unsigned hook = 0; hook < 2; ++hook) { 29 for (unsigned trial = 0; trial < sizeof(cases) / sizeof(cases[0]); ++trial) { 30 memset((void *)ADDR(0x50000000), 0, 0x50000); 31 MEM8(0x50001000) = cases[trial].literal_operation; 32 MEM8(0x50001001) = 1; 33 MEM32(0x50001004) = cases[trial].length; 34 MEM8(0x50002000) = cases[trial].distance_operation; 35 MEM8(0x50002001) = 1; 36 MEM32(0x50002004) = cases[trial].distance; 37 MEM32(0x5000401c) = cases[trial].initial_bits; 38 MEM32(0x50004028) = 0x5000fffd; 39 MEM32(0x5000402c) = 0x50017ffd; 40 MEM32(0x50004030) = 0x50010101; 41 MEM32(0x50004034) = 0x5000fffe; 42 MEM32(0x50004100) = 0x5002fff7; 43 MEM32(0x50004104) = 10; 44 MEM32(0x50004108) = 0xffffffff; 45 memset((void *)ADDR(0x5000fffe), 0xa5, 259); 46 MEM8(0x5000fffd) = 'A'; 47 MEM8(0x50017ffb) = 'X'; 48 MEM8(0x50017ffc) = 'Y'; 49 g_esp = 0x5004f008; 50 g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456; 51 MEM32(g_esp + 4) = 1; MEM32(g_esp + 8) = 1; 52 MEM32(g_esp + 12) = 0x50001000; MEM32(g_esp + 16) = 0x50002000; 53 MEM32(g_esp + 20) = 0x50004000; MEM32(g_esp + 24) = 0x50004100; 54 recomp_yield_hook = hook ? observe_yield : NULL; 55 g_backedges = RECOMP_YIELD_EVERY - 1; 56 yields = 0; 57 58 sub_250C1F70(); 59 60 unsigned consumed = cases[trial].initial_bits ? 0 : 1; 61 assert(MEM32(0x5000401c) == cases[trial].final_bits); 62 assert(MEM32(0x50004020) == 0); 63 assert(MEM32(0x50004034) == 0x5000fffe + cases[trial].output_bytes); 64 assert(MEM32(0x50004100) == 0x5002fff7 + consumed); 65 assert(MEM32(0x50004104) == 10 - consumed); 66 assert(MEM32(0x50004108) == 0xffffffff + consumed); 67 assert(MEM32(0x50004118) == cases[trial].message); 68 for (unsigned i = 0; i < cases[trial].output_bytes; ++i) 69 assert(MEM8(0x5000fffe + i) == (trial == 4 ? "XYAXY"[i] : 'A')); 70 assert(MEM8(0x5000fffd) == 'A'); 71 assert(MEM8(0x5000fffe + cases[trial].output_bytes) == 0xa5); 72 assert(g_esp == 0x5004f00c && g_ebp == 123); 73 assert(g_ebx == 234 && g_esi == 345 && g_edi == 456); 74 assert(g_eax == cases[trial].result && g_ecx == consumed); 75 assert(g_edx == 0x5000fffe + cases[trial].output_bytes); 76 assert(g_flag_k == (cases[trial].result ? FK_ADD : FK_TEST)); 77 assert(g_flag_a == (cases[trial].result ? 0xffffffff : 0)); 78 assert(g_flag_b == (cases[trial].result ? consumed : 0)); 79 assert(g_flag_cf == !!cases[trial].result); 80 unsigned did_yield = hook && cases[trial].edges; 81 assert(g_backedges == RECOMP_YIELD_EVERY - 1 + cases[trial].edges - did_yield * RECOMP_YIELD_EVERY); 82 assert(yields == did_yield); 83 } 84 } 85 }