aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/tests/atoi.c (3079B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #ifdef _WIN32
      6 #include <windows.h>
      7 static int (__cdecl *convert)(const char *);
      8 static int *error_number;
      9 #else
     10 #include "guest_crt.h"
     11 #include "guest_memory.h"
     12 recomp_func_t recomp_lookup(uint32_t address) { (void)address; abort(); }
     13 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
     14 #endif
     15 
     16 static const char *conversion;
     17 
     18 static uint32_t call(const char *input)
     19 {
     20 #ifdef _WIN32
     21     *error_number = 77;
     22     uint32_t result = convert(input);
     23     assert(*error_number == 0);
     24     return result;
     25 #else
     26     strcpy((char *)ADDR(0x50000100u), input);
     27     MEM32(guest_crt_errno_address()) = 77;
     28     g_esp = 0x5000ff00u;
     29     MEM32(g_esp + 4) = 0x50000100u;
     30     g_ebx = 0x12345678u; g_esi = 0x23456789u;
     31     g_edi = 0x3456789au; g_ebp = 0x456789abu;
     32     guest_crt_lookup(conversion)();
     33     assert(g_esp == 0x5000ff04u && MEM32(guest_crt_errno_address()) == 0);
     34     assert(g_ebx == 0x12345678u && g_esi == 0x23456789u);
     35     assert(g_edi == 0x3456789au && g_ebp == 0x456789abu);
     36     return g_eax;
     37 #endif
     38 }
     39 
     40 int main(void)
     41 {
     42 #ifdef _WIN32
     43     HMODULE crt = LoadLibraryA("msvcrt.dll");
     44     int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(crt, "_errno");
     45     assert(get_errno);
     46     error_number = get_errno();
     47 #else
     48     assert(guest_map("atoi test", 0x50000000u, 0x10000u));
     49     g_fs_base = 0x50000000u;
     50 #endif
     51     const struct { const char *input; uint32_t expected; } cases[] = {
     52         {"", 0}, {"  -42junk", 0xffffffd6u}, {"0x10", 0},
     53         {"2147483647", 0x7fffffffu}, {"2147483648", 0x80000000u},
     54         {"4294967295", 0xffffffffu}, {"4294967296", 0},
     55         {"-2147483649", 0x7fffffffu}, {"9223372036854775808", 0},
     56         {"184467440737095516160", 0}, {"-999999999999999999999", 559939585},
     57         {"+", 0}, {"\v\f\r\n\t 7", 7}, {"\xa0" "42", 0}
     58     };
     59     const char *names[] = {"atoi", "atol"};
     60     for (unsigned name = 0; name < 2; ++name) {
     61         conversion = names[name];
     62 #ifdef _WIN32
     63         convert = (void *)GetProcAddress(crt, conversion);
     64         assert(convert);
     65 #endif
     66         uint32_t results[14], digest = 2166136261u, random = 12345;
     67 
     68         for (unsigned i = 0; i < 14; ++i) results[i] = call(cases[i].input);
     69         for (unsigned c = 0; c < 256; ++c) {
     70             char input[] = {(char)c, '1', '2', '3', 0};
     71             digest = (digest ^ call(input)) * 16777619u;
     72         }
     73         for (unsigned i = 0; i < 4096; ++i) {
     74             char input[68];
     75             input[0] = i & 1 ? '+' : '-';
     76             for (unsigned j = 1; j < 65; ++j) {
     77                 random = random * 1664525u + 1013904223u;
     78                 input[j] = '0' + random % 10;
     79             }
     80             strcpy(input + 65, "x");
     81             digest = (digest ^ call(input)) * 16777619u;
     82         }
     83 
     84         for (unsigned i = 0; i < 14; ++i) assert(results[i] == cases[i].expected);
     85         assert(digest == 0xc691d92du);
     86         printf("%s: 4366 cases, digest %08x, decimal prefixes, 32-bit wrap, errno and ABI passed\n", conversion, digest);
     87     }
     88     return 0;
     89 }