Implementation:NVIDIA DALI SmallVector Tests
| Knowledge Sources | |
|---|---|
| Domains | Core, Testing |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
This file contains the Google Test suite for the SmallVector container class, verifying correct behavior for static storage, dynamic allocation, copy/move semantics, insertion, erasure, and resize operations.
Description
The SmallVector test suite exercises DALI's stack-optimized small vector implementation. SmallVector<T, N> stores up to N elements inline without heap allocation, automatically switching to dynamic storage when the element count exceeds the static capacity. The tests verify this transition is seamless and resource-safe.
A custom TestObj helper class with static reference counting (total and zombies) is used throughout the test file to track object lifetimes. This ensures that constructors, destructors, copy operations, and move operations are invoked the correct number of times, and that no memory leaks or double-destructions occur during any operation.
The test file validates both plain-old-data (PoD) types and non-trivial types, covering edge cases such as insertion with reallocation, move semantics that transfer dynamic buffer ownership, cross-size copy/move construction, conversion to/from std::vector, and initializer list construction.
Usage
Use these tests as a reference when modifying the SmallVector class or when writing code that relies on its behavior. Run this test binary to confirm that SmallVector correctly manages memory, element lifetimes, and transitions between static and dynamic storage.
Code Reference
Source Location
- Repository: NVIDIA_DALI
- File: dali/core/small_vector_test.cc
- Lines: 1-433
Signature
// Static assertions verifying SmallVector properties
static_assert(SmallVector<int, 6>::static_size == 6, "...");
static_assert(sizeof(SmallVector<int, 7>) >= 7*sizeof(int) + sizeof(size_t), "...");
// Helper class used for lifetime tracking
struct TestObj {
TestObj();
TestObj(int value);
~TestObj();
TestObj(const TestObj &other);
TestObj(TestObj &&other);
TestObj &operator=(const TestObj &other);
TestObj &operator=(TestObj &&other);
bool operator==(const TestObj &other) const;
int value = 0;
bool zombie = false;
static size_t total, zombies;
};
// SmallVector class under test
template <typename T, size_t static_size_, typename allocator>
class SmallVector;
Import
#include "dali/core/small_vector.h"
#include <gtest/gtest.h>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| N/A | N/A | N/A | Test file has no runtime inputs; all test data is generated inline within each TEST macro |
Outputs
| Name | Type | Description |
|---|---|---|
| Test results | Google Test assertions | PASS/FAIL verdicts for each test case validating SmallVector behavior |
Usage Examples
Static storage (no heap allocation)
SmallVector<TestObj, 5> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);
a.push_back(5);
EXPECT_EQ(a.size(), 5);
EXPECT_EQ(a.capacity(), 5);
EXPECT_FALSE(a.is_dynamic());
Dynamic growth beyond static capacity
SmallVector<TestObj, 3> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
EXPECT_FALSE(a.is_dynamic());
a.push_back(4); // triggers dynamic allocation
EXPECT_TRUE(a.is_dynamic());
EXPECT_EQ(a.size(), 4);
EXPECT_EQ(a.capacity(), 6);
Insertion, erase, and PoD operations
SmallVector<int, 3> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(7);
v.insert(v.begin() + 1, 2);
v.insert(v.begin() + 3, 4);
v.erase(v.begin()+2, v.end()-2);
ASSERT_EQ(v.size(), 4);
Conversion to/from std::vector
std::vector<TestObj> vec = { 1, 2, 3, 4 };
SmallVector<TestObj, 3> v1 = vec;
// ...
SmallVector<TestObj, 3> sv = { 1, 2, 3 };
std::vector<TestObj> result = sv.to_vector();