From 2d3392f22c894957d17dd13486e0565c4ecea89c Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Fri, 27 May 2022 17:28:05 -0400 Subject: Added NativeStringType (#2252) * #include an absolute path didn't work - because paths were taken to always be relative. * Use TerminatedUnownedStringSlice for literals in output C++. * Remove Escape/Unescape functions used in slang-token-reader.cpp Add target type of 'host-cpp' etc to map to the target types. * Fix some corner cases around string encoding. * Added unit test for string escaping. Fixed some assorted escaping bugs. * Updated test output. * Added decode test. * Stop using hex output, to get around 'greedy' aspect. Use octal instead. --- .../slang-unit-test/unit-test-offset-container.cpp | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 tools/slang-unit-test/unit-test-offset-container.cpp (limited to 'tools/slang-unit-test/unit-test-offset-container.cpp') diff --git a/tools/slang-unit-test/unit-test-offset-container.cpp b/tools/slang-unit-test/unit-test-offset-container.cpp new file mode 100644 index 000000000..9d8e3a9ff --- /dev/null +++ b/tools/slang-unit-test/unit-test-offset-container.cpp @@ -0,0 +1,117 @@ +// unit-test-offset-container.cpp + +#include "../../source/core/slang-offset-container.h" + +#include "tools/unit-test/slang-unit-test.h" + +using namespace Slang; + +static void _checkEncodeDecode(uint32_t size) +{ + uint8_t encode[OffsetString::kMaxSizeEncodeSize]; + + size_t encodeSize = OffsetString::calcEncodedSize(size, encode); + + size_t decodedSize; + const char* chars = OffsetString::decodeSize((const char*)encode, decodedSize); + + SLANG_CHECK(decodedSize == size); + SLANG_CHECK(chars - (const char*)encode == encodeSize); +} + +namespace { // anonymous + +struct Root +{ + Offset32Array > dirs; + Offset32Ptr name; + float value; +}; + +} // anonymous + +SLANG_UNIT_TEST(offsetContainer) +{ + _checkEncodeDecode(253); + + for (int64_t i = 0; i < 0x100000000; i += (i / 2) + 1) + { + _checkEncodeDecode(uint32_t(i)); + } + + { + OffsetContainer container; + + const char* strings[] = + { + "Hello", + "World", + nullptr, + }; + + { + auto& base = container.asBase(); + + Offset32Ptr root = container.newObject(); + + auto array = container.newArray>(SLANG_COUNT_OF(strings)); + for (Int i = 0; i < SLANG_COUNT_OF(strings); ++i) + { + base[array[i]] = container.newString(strings[i]); + } + base[root]->dirs = array; + } + + { + List copy; + copy.addRange(container.getData(), container.getDataCount()); + + MemoryOffsetBase base; + base.set(copy.getBuffer(), copy.getCount()); + + Root* root = (Root*)(copy.getBuffer() + kStartOffset); + + SLANG_CHECK(root->dirs.getCount() == SLANG_COUNT_OF(strings)); + + Int count = root->dirs.getCount(); + for (Int i = 0; i < count; ++i) + { + OffsetString* str = base.asRaw(base.asRaw(root->dirs[i])); + + const char* check = strings[i]; + + if (check) + { + SLANG_CHECK(str != nullptr); + const char* strCstr = str->getCstr(); + SLANG_CHECK(strcmp(strCstr, check) == 0); + } + else + { + SLANG_CHECK(str == nullptr); + } + } + + { + Index index = 0; + for (const auto v : root->dirs) + { + OffsetString* str = base.asRaw(base.asRaw(v)); + const char* check = strings[index]; + if (check) + { + SLANG_CHECK(str != nullptr); + const char* strCstr = str->getCstr(); + SLANG_CHECK(strcmp(strCstr, check) == 0); + } + else + { + SLANG_CHECK(str == nullptr); + } + + index ++; + } + } + } + } +} -- cgit v1.2.3