From 4cb876f8019d8b6465766356c8e2381367a5ffd0 Mon Sep 17 00:00:00 2001 From: stotiks <47013208+stotiks@users.noreply.github.com> Date: Tue, 29 Oct 2024 02:25:29 +0200 Subject: [PATCH] test_write_bytes_vitest_gen --- test/CMakeLists.txt | 4 + test/test_write_bytes_vitest_gen.cpp | 321 +++++++++++++++++++++++++++ 2 files changed, 325 insertions(+) create mode 100644 test/test_write_bytes_vitest_gen.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0351f4c85..51a768b67 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,6 +12,8 @@ add_executable(test_encoding test_encoding.cpp) add_executable(test_execute vm/test_execute.cpp) add_executable(database_fill database_fill.cpp) +add_executable(test_write_bytes_vitest_gen test_write_bytes_vitest_gen.cpp) + add_executable(mmx_tests mmx_tests.cpp) add_executable(vm_engine_tests vm/engine_tests.cpp) add_executable(vm_storage_tests vm/storage_tests.cpp) @@ -33,3 +35,5 @@ target_link_libraries(mmx_tests mmx_iface) target_link_libraries(vm_engine_tests mmx_vm) target_link_libraries(vm_storage_tests mmx_vm) +target_link_libraries(test_write_bytes_vitest_gen mmx_iface) + diff --git a/test/test_write_bytes_vitest_gen.cpp b/test/test_write_bytes_vitest_gen.cpp new file mode 100644 index 000000000..793353994 --- /dev/null +++ b/test/test_write_bytes_vitest_gen.cpp @@ -0,0 +1,321 @@ +#include + +#include +#include + +using namespace mmx; + +template +void static print_test(const std::string& testname, const T& value, const std::string& jsvalue) { + std::vector buffer; + vnx::VectorOutputStream stream(&buffer); + vnx::OutputBuffer out(&stream); + write_field(out, "field_name", value); + out.flush(); + const auto cppHex = vnx::to_hex_string(buffer.data(), buffer.size()); + + const auto test = + "it(\"" + testname + "\", () => {\n" + " const wb = new WriteBytes();\n" + " wb.write_field(\"field_name\", " + jsvalue + ");\n" + " const jsHex = wb.buffer.toHex();\n" + " const cppHex = \"" + cppHex + "\";\n" + " assert.equal(jsHex, cppHex);\n" + "});"; + + std::cout << test << std::endl << std::endl; +} + +int main(int argc, char** argv) +{ + { + const auto testname = "bool true"; + constexpr auto value = true; + const std::string jsvalue = "true"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "bool false"; + constexpr auto value = false; + const std::string jsvalue = "false"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "uint64_t"; + constexpr auto value = (uint64_t)1337; + const std::string jsvalue = std::to_string(value) + "n"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "uint64_t min"; + constexpr auto value = std::numeric_limits::min(); + const std::string jsvalue = std::to_string(value) + "n"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "uint64_t max"; + constexpr auto value = std::numeric_limits::max(); + const std::string jsvalue = std::to_string(value) + "n"; + print_test(testname, value, jsvalue); + } + + //{ + // const auto testname = "uint32_t"; + // const auto value = (uint32_t)1337; + // const std::string jsvalue = std::to_string(value); + // print_test(testname, value, jsvalue); + //} + + //{ + // const auto testname = "uint16_t"; + // const auto value = (uint16_t)1337; + // const std::string jsvalue = std::to_string(value); + // print_test(testname, value, jsvalue); + //} + + //{ + // const auto testname = "uint8_t"; + // const auto value = (uint16_t)1337; + // const std::string jsvalue = std::to_string(value); + // print_test(testname, value, jsvalue); + //} + + { + const auto testname = "int64_t"; + constexpr auto value = (int64_t)1337; + const std::string jsvalue = std::to_string(value) + "n"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "int64_t min"; + constexpr auto value = std::numeric_limits::min(); + const std::string jsvalue = std::to_string(value) + "n"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "int64_t max"; + constexpr auto value = std::numeric_limits::max(); + const std::string jsvalue = std::to_string(value) + "n"; + print_test(testname, value, jsvalue); + } + + //{ + // const auto testname = "int32_t"; + // const auto value = (int32_t)1337; + // const std::string jsvalue = std::to_string(value); + // print_test(testname, value, jsvalue); + //} + + //{ + // const auto testname = "int16_t"; + // const auto value = (int16_t)1337; + // const std::string jsvalue = std::to_string(value); + // print_test(testname, value, jsvalue); + //} + + //{ + // const auto testname = "int8_t"; + // const auto value = (int16_t)1337; + // const std::string jsvalue = std::to_string(value); + // print_test(testname, value, jsvalue); + //} + + // TODO uint128 + + { + const auto testname = "string"; + const std::string value = "string"; + const std::string jsvalue = "\"" + value + "\""; + print_test(testname, value, jsvalue); + } + + const auto test_vector = std::vector{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + + { + const auto testname = "bytes_t"; + const auto value = bytes_t<16>(test_vector); + const std::string jsvalue = "new bytes_t(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]))"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "bytes_t empty"; + const auto value = bytes_t<0>(); + const std::string jsvalue = "new bytes_t()"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "bytes_t addr_t"; + const auto value = addr_t("mmx1ckyz0x7fpet4y7zmckyg7lklp8dc5gdr2kjd8hamk49rnk8zu9eq2cnz7a"); + const std::string jsvalue = "new addr_t(\"mmx1ckyz0x7fpet4y7zmckyg7lklp8dc5gdr2kjd8hamk49rnk8zu9eq2cnz7a\")"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "vector"; + const auto value = test_vector; + const std::string jsvalue = "new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "vector string"; + const auto value = std::vector{ "1337", "hello", "world"}; + const std::string jsvalue = "[\"1337\", \"hello\", \"world\"]"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "vector uint64_t"; + const auto value = std::vector{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + const std::string jsvalue = "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "vector empty"; + const auto value = std::vector{}; + const std::string jsvalue = "new Uint8Array([])"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "Variant empty"; + const vnx::Variant value; + const std::string jsvalue = "new Variant()"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "Variant bool true"; + const vnx::Variant value(true); + const std::string jsvalue = "new Variant(true)"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "Variant bool false"; + const vnx::Variant value(false); + const std::string jsvalue = "new Variant(false)"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "Variant int"; + const vnx::Variant value(255); + const std::string jsvalue = "new Variant(255)"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "Variant string"; + const vnx::Variant value("1337"); + const std::string jsvalue = "new Variant(\"1337\")"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "Variant vector"; + const vnx::Variant value(test_vector); + const std::string jsvalue = "new Variant([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "Variant vnx::Object"; + vnx::Object obj; + obj["field1"] = "1337"; + obj["field2"] = (uint64_t)1337; + const vnx::Variant value(obj); + const std::string jsvalue = "new Variant(new vnxObject({field1: \"1337\", field2: 1337}))"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "vnx::Object"; + vnx::Object value; + value["field1"] = "1337"; + value["field2"] = (uint64_t)1337; + const std::string jsvalue = "new vnxObject({field1: \"1337\", field2: 1337})"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "vnx::Object empty"; + const vnx::Object value; + const std::string jsvalue = "new vnxObject()"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "txout_t"; + txout_t value; + value.address = addr_t("mmx1ckyz0x7fpet4y7zmckyg7lklp8dc5gdr2kjd8hamk49rnk8zu9eq2cnz7a"); + value.contract = addr_t(); + value.amount = 255; + value.memo = "memo"; + + const std::string jsvalue = "new txout_t({address: \"mmx1ckyz0x7fpet4y7zmckyg7lklp8dc5gdr2kjd8hamk49rnk8zu9eq2cnz7a\", contract : \"mmx1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqdgytev\", amount : 255,memo : \"memo\"})"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "txin_t"; + txin_t value; + value.address = addr_t("mmx1ckyz0x7fpet4y7zmckyg7lklp8dc5gdr2kjd8hamk49rnk8zu9eq2cnz7a"); + value.contract = addr_t(); + value.amount = 255; + value.memo = "memo"; + value.solution = 255; + value.flags = 255; + + const std::string jsvalue = "new txin_t({address: \"mmx1ckyz0x7fpet4y7zmckyg7lklp8dc5gdr2kjd8hamk49rnk8zu9eq2cnz7a\", contract : \"mmx1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqdgytev\", amount : 255,memo : \"memo\", solution: 255, flags: 255 })"; + print_test(testname, value, jsvalue); + } + + // TODO ulong_fraction_t time_segment_t compile_flags_t method_t + + { + const auto testname = "optional"; + const auto value = vnx::optional>(test_vector); + const std::string jsvalue = "new optional(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]))"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "optional nullptr"; + const auto value = vnx::optional>(nullptr); + const std::string jsvalue = "new optional(null)"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "pair"; + std::pair value("test1", 255); + const std::string jsvalue = "new pair(\"test1\", 255)"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "map"; + std::map value = { {"test1", 255}, {"test2", 255} }; + const std::string jsvalue = "new Map([ [\"test1\", 255], [\"test2\", 255] ])"; + print_test(testname, value, jsvalue); + } + + { + const auto testname = "map empty"; + std::map value = {}; + const std::string jsvalue = "new Map([])"; + print_test(testname, value, jsvalue); + } + +} +