summaryrefslogtreecommitdiff
path: root/tools/slang-test
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-11-08 09:13:44 -0500
committerGitHub <noreply@github.com>2019-11-08 09:13:44 -0500
commit0ac010a72e777b2c284583fcb8554abee83d8ff5 (patch)
tree7133e5c3117c180d19ce11841187ff3f9d2ec5fe /tools/slang-test
parent99c295477fa1f6c5ce47e0d1c8fb3eea9d5e5f98 (diff)
Riff Container Stream Writing (#1116)
* * Added option to get random bytes from RandomGenerator * Added ability to allocate only in current block on MemoryArena * Allowed RiffContainer to not allocate new Data blocks, if can just extend the Data it has (because it's at the end of current block and there is space for the new data). * Added coverage for change on Riff unit test * Add test coverage for allocations over multiple Data blocks. * Improve comment on riff unit tests.
Diffstat (limited to 'tools/slang-test')
-rw-r--r--tools/slang-test/unit-test-riff.cpp84
1 files changed, 79 insertions, 5 deletions
diff --git a/tools/slang-test/unit-test-riff.cpp b/tools/slang-test/unit-test-riff.cpp
index 58e9a1bb7..0fb81113a 100644
--- a/tools/slang-test/unit-test-riff.cpp
+++ b/tools/slang-test/unit-test-riff.cpp
@@ -2,24 +2,50 @@
#include "../../source/core/slang-riff.h"
+#include "../../source/core/slang-random-generator.h"
+
#include "test-context.h"
using namespace Slang;
+static void _writeRandom(RandomGenerator* rand, size_t maxSize, RiffContainer& ioContainer, List<uint8_t>& ioData)
+{
+ while (true)
+ {
+ const Index oldCount = ioData.getCount();
+
+ const size_t allocSize = size_t(rand->nextInt32InRange(1, 50));
+
+ if (allocSize + oldCount > maxSize)
+ {
+ break;
+ }
+
+ ioData.setCount(oldCount + Index(allocSize));
+ rand->nextData(ioData.getBuffer() + oldCount, allocSize);
+
+ // Write
+ ioContainer.write(ioData.getBuffer() + oldCount, allocSize);
+ }
+
+ // Should be a single block with same data as the List
+ RiffContainer::DataChunk* dataChunk = as<RiffContainer::DataChunk>(ioContainer.getCurrentChunk());
+ SLANG_ASSERT(dataChunk);
+}
+
static void riffUnitTest()
{
+ typedef RiffContainer::ScopeChunk ScopeChunk;
+ typedef RiffContainer::Chunk::Kind Kind;
+
const FourCC markThings = SLANG_FOUR_CC('T', 'H', 'I', 'N');
const FourCC markData = SLANG_FOUR_CC('D', 'A', 'T', 'A');
{
- typedef RiffContainer::ScopeChunk ScopeChunk;
- typedef RiffContainer::Chunk::Kind Kind;
-
RiffContainer container;
{
- ScopeChunk scopeContainer(&container, Kind::List, markThings);
-
+ ScopeChunk scopeContainer(&container, Kind::List, markThings);
{
ScopeChunk scopeChunk(&container, Kind::Data, markData);
@@ -85,6 +111,54 @@ static void riffUnitTest()
}
+ // Test writing as a stream only allocates a single data block (as long as there is enough space).
+ {
+ RiffContainer container;
+
+ ScopeChunk scopeChunk(&container, Kind::List, markData);
+ {
+ ScopeChunk scopeChunk(&container, Kind::Data, markData);
+ RefPtr<RandomGenerator> rand = RandomGenerator::create(0x345234);
+
+ List<uint8_t> data;
+ _writeRandom(rand, container.getMemoryArena().getBlockPayloadSize() / 2, container, data);
+
+ // Should be a single block with same data as the List
+ RiffContainer::DataChunk* dataChunk = as<RiffContainer::DataChunk>(container.getCurrentChunk());
+ SLANG_ASSERT(dataChunk);
+
+ // It should be a single block
+ SLANG_CHECK(dataChunk->getSingleData() != nullptr);
+
+ SLANG_CHECK(dataChunk->isEqual(data.getBuffer(), data.getCount()));
+
+ }
+ }
+
+ // Test writing across multiple data blocks
+ {
+ RefPtr<RandomGenerator> rand = RandomGenerator::create(0x345234);
+
+ for (Int i = 0 ; i < 100; ++i)
+ {
+ RiffContainer container;
+
+ const size_t maxSize = rand->nextInt32InRange(1, int32_t(container.getMemoryArena().getBlockPayloadSize() * 3));
+
+ ScopeChunk scopeChunk(&container, Kind::List, markData);
+ {
+ ScopeChunk scopeChunk(&container, Kind::Data, markData);
+
+ List<uint8_t> data;
+ _writeRandom(rand, maxSize, container, data);
+
+ // Should be a single block with same data as the List
+ RiffContainer::DataChunk* dataChunk = as<RiffContainer::DataChunk>(container.getCurrentChunk());
+ SLANG_CHECK(dataChunk && dataChunk->isEqual(data.getBuffer(), data.getCount()));
+ }
+ }
+ }
+
#if 0
{
RiffContainer container;