| 1 | #include "cpp/data_lang.h"
|
| 2 |
|
| 3 | #include "_gen/core/value.asdl.h"
|
| 4 | #include "data_lang/j8_libc.h" // for comparison
|
| 5 | #include "data_lang/j8_test_lib.h"
|
| 6 | #include "vendor/greatest.h"
|
| 7 |
|
| 8 | TEST PartIsUtf8_test() {
|
| 9 | BigStr* s = StrFromC("hi");
|
| 10 |
|
| 11 | ASSERT(pyj8::PartIsUtf8(s, 0, 2));
|
| 12 |
|
| 13 | // empty string is trivially UTF-8
|
| 14 | ASSERT(pyj8::PartIsUtf8(s, 0, 0));
|
| 15 |
|
| 16 | BigStr* binary = StrFromC("h\xff");
|
| 17 | ASSERT(!pyj8::PartIsUtf8(binary, 0, len(binary)));
|
| 18 |
|
| 19 | // first byte is UTF-8
|
| 20 | ASSERT(pyj8::PartIsUtf8(binary, 0, 1));
|
| 21 | // second byte isn't
|
| 22 | ASSERT(!pyj8::PartIsUtf8(binary, 1, 2));
|
| 23 |
|
| 24 | PASS();
|
| 25 | }
|
| 26 |
|
| 27 | // TODO: remove duplication
|
| 28 | #define LOSSY_JSON (1 << 3)
|
| 29 |
|
| 30 | TEST WriteString_test() {
|
| 31 | auto buf = Alloc<mylib::BufWriter>();
|
| 32 |
|
| 33 | for (int i = 0; J8_TEST_CASES[i]; ++i) {
|
| 34 | const char* s = J8_TEST_CASES[i];
|
| 35 | BigStr* s2 = StrFromC(s);
|
| 36 |
|
| 37 | buf = Alloc<mylib::BufWriter>();
|
| 38 | pyj8::WriteString(s2, LOSSY_JSON, buf);
|
| 39 |
|
| 40 | BigStr* result = buf->getvalue();
|
| 41 | log("result = %s", result->data_);
|
| 42 |
|
| 43 | buf = Alloc<mylib::BufWriter>();
|
| 44 | pyj8::WriteString(s2, 0, buf);
|
| 45 |
|
| 46 | result = buf->getvalue();
|
| 47 | log("result = %s", result->data_);
|
| 48 | }
|
| 49 |
|
| 50 | PASS();
|
| 51 | }
|
| 52 |
|
| 53 | TEST compare_c_test() {
|
| 54 | // Compare two implementations
|
| 55 |
|
| 56 | auto buf = Alloc<mylib::BufWriter>();
|
| 57 |
|
| 58 | for (int i = 0; J8_TEST_CASES[i]; ++i) {
|
| 59 | const char* s = J8_TEST_CASES[i];
|
| 60 | int input_len = strlen(s);
|
| 61 | j8_buf_t in = {(unsigned char*)s, input_len};
|
| 62 |
|
| 63 | j8_buf_t c_result = {0};
|
| 64 | J8EncodeString(in, &c_result, 0);
|
| 65 |
|
| 66 | printf("c_result %s\n", c_result.data);
|
| 67 | printf("c_result.len %d\n", c_result.len);
|
| 68 |
|
| 69 | BigStr* s2 = StrFromC(s);
|
| 70 |
|
| 71 | buf = Alloc<mylib::BufWriter>();
|
| 72 | pyj8::WriteString(s2, LOSSY_JSON, buf);
|
| 73 |
|
| 74 | BigStr* cpp_result = buf->getvalue();
|
| 75 |
|
| 76 | // Equal lengths
|
| 77 | ASSERT_EQ_FMT(c_result.len, len(cpp_result), "%d");
|
| 78 | // Equal contents
|
| 79 | ASSERT(memcmp(c_result.data, cpp_result->data_, c_result.len) == 0);
|
| 80 |
|
| 81 | free(c_result.data);
|
| 82 |
|
| 83 | //
|
| 84 | // Encode again with J8 fallback
|
| 85 | //
|
| 86 |
|
| 87 | c_result = {0};
|
| 88 | J8EncodeString(in, &c_result, 1);
|
| 89 |
|
| 90 | printf("c_result %s\n", c_result.data);
|
| 91 | printf("c_result.len %d\n", c_result.len);
|
| 92 |
|
| 93 | buf = Alloc<mylib::BufWriter>();
|
| 94 | pyj8::WriteString(s2, 0, buf);
|
| 95 |
|
| 96 | cpp_result = buf->getvalue();
|
| 97 |
|
| 98 | // Equal lengths
|
| 99 | ASSERT_EQ_FMT(c_result.len, len(cpp_result), "%d");
|
| 100 | // Equal contents
|
| 101 | ASSERT(memcmp(c_result.data, cpp_result->data_, c_result.len) == 0);
|
| 102 |
|
| 103 | free(c_result.data);
|
| 104 |
|
| 105 | printf("\n");
|
| 106 | }
|
| 107 |
|
| 108 | PASS();
|
| 109 | }
|
| 110 |
|
| 111 | using value_asdl::value;
|
| 112 | using value_asdl::value_t;
|
| 113 |
|
| 114 | TEST heap_id_test() {
|
| 115 | value_t* val1 = Alloc<value::Str>(kEmptyString);
|
| 116 | value_t* val2 = Alloc<value::Str>(kEmptyString);
|
| 117 |
|
| 118 | int id1 = j8::HeapValueId(val1);
|
| 119 | int id2 = j8::HeapValueId(val2);
|
| 120 |
|
| 121 | log("id1 = %d, id2 = %d", id1, id2);
|
| 122 | ASSERT(id1 != id2);
|
| 123 |
|
| 124 | PASS();
|
| 125 | }
|
| 126 |
|
| 127 | GREATEST_MAIN_DEFS();
|
| 128 |
|
| 129 | int main(int argc, char** argv) {
|
| 130 | gHeap.Init();
|
| 131 |
|
| 132 | GREATEST_MAIN_BEGIN();
|
| 133 |
|
| 134 | RUN_TEST(PartIsUtf8_test);
|
| 135 | RUN_TEST(WriteString_test);
|
| 136 | RUN_TEST(compare_c_test);
|
| 137 | RUN_TEST(heap_id_test);
|
| 138 |
|
| 139 | gHeap.CleanProcessExit();
|
| 140 |
|
| 141 | GREATEST_MAIN_END();
|
| 142 | return 0;
|
| 143 | }
|