diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-10-24 17:58:24 -0400 |
|---|---|---|
| committer | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-10-24 14:58:24 -0700 |
| commit | 89ddb50eaccc1b7b590dbde55032721762711fb2 (patch) | |
| tree | e61da2c1604e0d52d3a9915363769ccf950b62f3 /tools | |
| parent | 58ad4b1a9ca43098a071c42bd752a4a48405bf0e (diff) | |
OffsetContainer serialization (#1093)
* OffsetContainer with unit tests.
* State serialization working with OffsetContainer.
* Fixes to make work with OffsetContainer.
* Added OffsetContainer documentation.
* Remove RelativeContainer.
* Fix problem with + on Offset32Ptr on windows x86 target.
* * Made OffsetBase a base class of OffsetContainer.
* Added MemoryOffsetBase to just handle being a chunk of memory.
* * Use operator[] to access contents of OffsetContainer
* Fix the type hash to work across different size_t sizes.
* Fixed some Offset type related comments.
* Fix bug around using asBase, because it returns a reference just using 'auto' will means it becomes a value type.
Remove assignment and copy ctor from OffsetBase.
* Evaluation order of assignment can lead to wrong behavior with Offset32Ptr/raw pointers. Document the fact, and fix in StateSerializeUtil.
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/slang-test/slang-test.vcxproj | 2 | ||||
| -rw-r--r-- | tools/slang-test/slang-test.vcxproj.filters | 2 | ||||
| -rw-r--r-- | tools/slang-test/unit-offset-container.cpp | 119 | ||||
| -rw-r--r-- | tools/slang-test/unit-relative-container.cpp | 94 |
4 files changed, 121 insertions, 96 deletions
diff --git a/tools/slang-test/slang-test.vcxproj b/tools/slang-test/slang-test.vcxproj index d1326d655..b22a3fe60 100644 --- a/tools/slang-test/slang-test.vcxproj +++ b/tools/slang-test/slang-test.vcxproj @@ -175,7 +175,7 @@ <ClCompile Include="slangc-tool.cpp" /> <ClCompile Include="test-context.cpp" /> <ClCompile Include="test-reporter.cpp" /> - <ClCompile Include="unit-relative-container.cpp" /> + <ClCompile Include="unit-offset-container.cpp" /> <ClCompile Include="unit-test-byte-encode.cpp" /> <ClCompile Include="unit-test-free-list.cpp" /> <ClCompile Include="unit-test-memory-arena.cpp" /> diff --git a/tools/slang-test/slang-test.vcxproj.filters b/tools/slang-test/slang-test.vcxproj.filters index 9bf6f9b26..b88490559 100644 --- a/tools/slang-test/slang-test.vcxproj.filters +++ b/tools/slang-test/slang-test.vcxproj.filters @@ -44,7 +44,7 @@ <ClCompile Include="test-reporter.cpp"> <Filter>Source Files</Filter> </ClCompile> - <ClCompile Include="unit-relative-container.cpp"> + <ClCompile Include="unit-offset-container.cpp"> <Filter>Source Files</Filter> </ClCompile> <ClCompile Include="unit-test-byte-encode.cpp"> diff --git a/tools/slang-test/unit-offset-container.cpp b/tools/slang-test/unit-offset-container.cpp new file mode 100644 index 000000000..d0990d9bb --- /dev/null +++ b/tools/slang-test/unit-offset-container.cpp @@ -0,0 +1,119 @@ +// unit-test-path.cpp + +#include "../../source/core/slang-offset-container.h" + +#include "test-context.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<Offset32Ptr<OffsetString> > dirs; + Offset32Ptr<OffsetString> name; + float value; +}; + +} // anonymous + +static void offsetContainerUnitTest() +{ + _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> root = container.newObject<Root>(); + + auto array = container.newArray<Offset32Ptr<OffsetString>>(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<uint8_t> 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 ++; + } + } + } + } +} + +SLANG_UNIT_TEST("OffsetContainer", offsetContainerUnitTest); diff --git a/tools/slang-test/unit-relative-container.cpp b/tools/slang-test/unit-relative-container.cpp deleted file mode 100644 index a9ff7c0fe..000000000 --- a/tools/slang-test/unit-relative-container.cpp +++ /dev/null @@ -1,94 +0,0 @@ -// unit-test-path.cpp - -#include "../../source/core/slang-relative-container.h" - -#include "test-context.h" - -using namespace Slang; - -static void _checkEncodeDecode(uint32_t size) -{ - uint8_t encode[RelativeString::kMaxSizeEncodeSize]; - - size_t encodeSize = RelativeString::calcEncodedSize(size, encode); - - size_t decodedSize; - const char* chars = RelativeString::decodeSize((const char*)encode, decodedSize); - - SLANG_CHECK(decodedSize == size); - SLANG_CHECK(chars - (const char*)encode == encodeSize); -} - -namespace { // anonymous - -struct Root -{ - Relative32Array<Relative32Ptr<RelativeString> > dirs; - Relative32Ptr<RelativeString> name; - float value; -}; - -} // anonymous - -static void relativeContainerUnitTest() -{ - _checkEncodeDecode(253); - - for (int64_t i = 0; i < 0x100000000; i += (i / 2) + 1) - { - _checkEncodeDecode(uint32_t(i)); - } - - { - RelativeContainer container; - - const char* strings[] = - { - "Hello", - "World", - nullptr, - }; - - { - Safe32Ptr<Root> root = container.newObject<Root>(); - - auto array = container.newArray<Relative32Ptr<RelativeString>>(SLANG_COUNT_OF(strings)); - for (Int i = 0; i < SLANG_COUNT_OF(strings); ++i) - { - array[i] = container.newString(strings[i]); - } - - root->dirs = array; - } - - { - RelativeContainer copy; - copy.set(container.getData(), container.getDataCount()); - - Root* root = (Root*)copy.getData(); - - SLANG_CHECK(root->dirs.getCount() == SLANG_COUNT_OF(strings)); - - Int count = root->dirs.getCount(); - for (Int i = 0; i < count; ++i) - { - RelativeString* str = 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); - } - } - } - } -} - -SLANG_UNIT_TEST("RelativeContainer", relativeContainerUnitTest); |
