AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
ContainerTests.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGXUNIT_CONTAINERTESTS_H
18#define AGXUNIT_CONTAINERTESTS_H
19
21#include <agx/String.h>
22#include <fstream>
23#include <agx/Allocator.h>
24
25namespace agxUnit
26{
27
29 template <typename T>
31 {
32 public:
33 void *allocateBytes(size_t numBytes, size_t alignment = 16)
34 {
35 agxThrow std::runtime_error(agx::String::format("AlwaysFailingAllocator called with numBytes %lu\n and alignment %lu.", numBytes, alignment));
36 }
37
38 void deallocateBytes(void* /*buffer*/, size_t /*numBytes*/)
39 {
40 }
41
43 {
44 }
45 };
46
47
48
49 template<typename T>
56 void testBadAlloc(const agx::String& /*typeName*/)
57 {
58
59 AGXUNIT_BEGIN_TEST(typeName + ": badAlloc");
60 for (size_t i = 0; i < 4; ++i) {
61 // Create a simple T.
62 T t;
63 size_t oldCapacity = t.capacity();
64 void* oldPtr = (void*)t.ptr(); // Save to cast away the constness here, we will only use the pointer for comparisons.
65
66 bool hasBeenInException = false;
67
68 try {
69 // Here, we try to provoke an exception.
70 if (i % 2 == 0)
71 t.reserve(2);
72 else
73 insertElement(t);
74 }
75 catch (std::exception& e)
76 {
77 std::cout << "\nCaught (intentionally provoked) exception: " << e.what() << std::endl;
78 hasBeenInException = true;
79 }
80
81 AGXUNIT_ASSERT_MESSAGE("Did not Throw exception.", hasBeenInException);
82 AGXUNIT_ASSERT_MESSAGE("Size is not 0.", t.size() == 0);
83 AGXUNIT_ASSERT_MESSAGE("Capacity has changed by bad alloc.", t.capacity() == oldCapacity);
84 AGXUNIT_ASSERT_MESSAGE("Pointer has changed by bad alloc.", t.ptr() == oldPtr);
85
86 if (i < 2)
87 t.clear(); // Test deallocating once with clear, and once without.
88 }
89
90 AGXUNIT_END_TEST(typeName + ": badAlloc");
91 }
92
93
94
95 template<typename T>
101 void testAllocationOverflow(const agx::String& /*typeName*/)
102 {
103 // The purpose of this test is to test how the container recovers from bad alloc.
104 // 1. It should through an exception.
105 // 2. Otherwise, its state should be intact.
106
107 AGXUNIT_BEGIN_TEST(typeName + ": testAllocationOverflow");
108#if 0 // This test will fail in many containers. \todo: Fix containers!
109
110 for (size_t i = 0; i < 2; ++i) {
111 // Create a simple T.
112 T t;
113 insertElement(t);
114 t.reserve(2);
115 size_t oldCapacity = t.capacity();
116 void* oldPtr = (void*)t.ptr(); // Save to cast away the constness here, we will only use the pointer for comparisons.
117
118 bool hasBeenInException = false;
119 const size_t tooBigToAlloc = std::numeric_limits<size_t>::max() - 2;
120
121
122 try {
123 // Here, we try to provoke an exception.
124 t.reserve(tooBigToAlloc);
125
126 oldCapacity = t.capacity();
127 oldPtr = (void*)t.ptr(); // Save to cast away the constness here, we will only use the pointer for comparisons.
128 }
129 catch (std::exception& e)
130 {
131 std::cout << "\nCaught (intentionally provoked) exception: " << e.what() << std::endl;
132 hasBeenInException = true;
133 }
134 if (hasBeenInException) {
135 AGXUNIT_ASSERT_MESSAGE("Size is not 1.", t.size() == 1);
136 AGXUNIT_ASSERT_MESSAGE("Capacity has changed by bad alloc.", t.capacity() == oldCapacity);
137 AGXUNIT_ASSERT_MESSAGE("Pointer has changed by bad alloc.", t.ptr() == oldPtr);
138 }
139 else {
140 const size_t minNewCapacity = oldCapacity + tooBigToAlloc;
142 agx::String::format("Did not reserve enough capacity, wanted: %lu; reserved: %lu.\n", minNewCapacity, t.capacity()),
143 t.capacity() >= minNewCapacity);
144 }
145
146 AGXUNIT_ASSERT_MESSAGE("Did not Throw exception.", hasBeenInException);
147
148 if (i == 1)
149 t.clear(); // Test deallocating once with clear, and once without.
150 }
151#endif
152 AGXUNIT_END_TEST(typeName + ": testAllocationOverflow");
153 }
154
155}
156
157#endif
158
#define AGXUNIT_BEGIN_TEST(X)
Begin a new named test. Name (X) must be unique through all tests.
Definition UnitMacros.h:365
#define AGXUNIT_END_TEST(X)
End a named test.
Definition UnitMacros.h:371
#define AGXUNIT_ASSERT_MESSAGE(message, condition)
Report error (with additional message) if condition returns false.
Definition UnitMacros.h:376
Never allocates anything, throws always an exception. Only for testing.
void * allocateBytes(size_t numBytes, size_t alignment=16)
void deallocateBytes(void *, size_t)
void setContainer(class agx::Container *)
static AgXString format(const char *format,...)
C printf formatting of a string.
Definition String.h:471
Templated allocator.
Definition Allocator.h:109
The Container is the base class for several of the container classes proided by AGX,...
Definition Container.h:35
#define agxThrow
Definition debug.h:109
void testBadAlloc(const agx::String &)
The purpose of this test is to test how the container recovers from bad alloc.
void testAllocationOverflow(const agx::String &)
The purpose of this test is to test for overflow in allocation size.