2
2
3
3
#include " Test/TestManager.hpp"
4
4
5
+ #include " OpenGL/Types.hpp"
6
+
5
7
namespace Test
6
8
{
7
9
class GraphicsTester : public TestManager
@@ -11,5 +13,85 @@ namespace Test
11
13
12
14
void run_unit_tests () override ;
13
15
void run_performance_tests () override ;
16
+
17
+ private:
18
+
19
+ template <typename value_type>
20
+ void test_buffer ()
21
+ {
22
+ {SCOPE_SECTION (" Byte" )
23
+ OpenGL::Buffer byte_buffer = OpenGL::Buffer ({OpenGL::BufferStorageFlag::DynamicStorageBit}, sizeof (value_type) * 4 );
24
+ const auto arr = std::array<value_type, 2 >{ value_type{2 }, value_type{3 } };
25
+ const auto min_data = std::numeric_limits<value_type>::min ();
26
+ const auto max_data = std::numeric_limits<value_type>::max ();
27
+
28
+ {SCOPE_SECTION (" Baseline" )
29
+ bool caught_exception = false ;
30
+ try { byte_buffer.download_data_array <value_type, 4 >(); }
31
+ catch (const std::exception& e) { caught_exception = true ; }
32
+
33
+ // Buffer = {0, 0, 0, 0}, We expect the buffer to be zeroed out on creation.
34
+ // Cannot download data from an empty buffer so tets for thrown exception instead.
35
+ CHECK_TRUE (caught_exception, " Download data from empty buffer" );
36
+ CHECK_EQUAL (byte_buffer.used_capacity (), 0 , " Used capacity" );
37
+ CHECK_EQUAL (byte_buffer.capacity (), sizeof (value_type) * 4 , " Capacity" );
38
+ }
39
+
40
+ {SCOPE_SECTION (" Set index 0" )
41
+ byte_buffer.set_data (min_data);
42
+ // Buffer = {0, 0, 0, 0}, Set index 0 to min (0).
43
+ std::array<value_type, 1 > expected_result = { min_data };
44
+ auto result = byte_buffer.download_data_array <value_type, 1 >();
45
+
46
+ CHECK_CONTAINER_EQUAL (result, expected_result, " Check data after setting index 0" );
47
+ CHECK_EQUAL (byte_buffer.used_capacity (), sizeof (value_type), " Used capacity" );
48
+ CHECK_EQUAL (byte_buffer.capacity (), sizeof (value_type) * 4 , " Capacity" );
49
+ }
50
+ {SCOPE_SECTION (" Set index 1" )
51
+ byte_buffer.set_data (max_data);
52
+ // Buffer = {0, 255, 0, 0}, Set index 1 to max.
53
+ std::array<value_type, 2 > expected_result = { min_data, max_data };
54
+ auto result = byte_buffer.download_data_array <value_type, 2 >();
55
+
56
+ CHECK_CONTAINER_EQUAL (result, expected_result, " Check data after setting index 1" );
57
+ CHECK_EQUAL (byte_buffer.used_capacity (), sizeof (value_type) * 2 , " Used capacity" );
58
+ CHECK_EQUAL (byte_buffer.capacity (), sizeof (value_type) * 4 , " Capacity" );
59
+ }
60
+ {SCOPE_SECTION (" Set array to index 2" )
61
+ byte_buffer.set_data (arr);
62
+ std::array<value_type, 4 > expected_result = { min_data, max_data, arr[0 ], arr[1 ] };
63
+ // Buffer = {0, 255, 2, 3}, Set index 2 and 3 to array values.
64
+ auto result = byte_buffer.download_data_array <value_type, 4 >();
65
+
66
+ CHECK_CONTAINER_EQUAL (result, expected_result, " Check data after setting array" );
67
+ CHECK_EQUAL (byte_buffer.used_capacity (), sizeof (value_type) * 4 , " Used capacity" );
68
+ CHECK_EQUAL (byte_buffer.capacity (), sizeof (value_type) * 4 , " Capacity" );
69
+ }
70
+ {SCOPE_SECTION (" Clear index 1" )
71
+ byte_buffer.clear (sizeof (value_type), sizeof (value_type));
72
+ // Clear data not at the end, should not affect used capacity.
73
+ // V clearing this byte.
74
+ // Buffer = {0, 0, 2, 3}, Clear index 1.
75
+ std::array<value_type, 4 > expected_result_non_end_remove = { min_data, value_type{0 }, arr[0 ], arr[1 ] };
76
+ auto result = byte_buffer.download_data_array <value_type, 4 >();
77
+
78
+ CHECK_CONTAINER_EQUAL (result, expected_result_non_end_remove, " Clear byte in middle download data" );
79
+ CHECK_EQUAL (byte_buffer.used_capacity (), sizeof (value_type) * 4 , " Clear byte in middle used capacity" );
80
+ CHECK_EQUAL (byte_buffer.capacity (), sizeof (value_type) * 4 , " Capacity" );
81
+ }
82
+ {SCOPE_SECTION (" Clear index 3" )
83
+ byte_buffer.clear (3 * sizeof (value_type), sizeof (value_type));
84
+ // Clear data from the end should reduce used capacity.
85
+ // C V Despite clearing off the end, the value is still there in the current implementation.
86
+ // Buffer = {0, 0, 2, 0}, Clear index 3.
87
+ std::array<value_type, 3 > expected_result_end = { min_data, value_type{0 }, arr[0 ] };
88
+ auto result = byte_buffer.download_data_array <value_type, 3 >();
89
+
90
+ CHECK_CONTAINER_EQUAL (result, expected_result_end, " Clear byte at end download data" );
91
+ CHECK_EQUAL (byte_buffer.used_capacity (), sizeof (value_type) * 3 , " Clear byte at end used capacity" );
92
+ CHECK_EQUAL (byte_buffer.capacity (), sizeof (value_type) * 4 , " Capacity" );
93
+ }
94
+ }
95
+ }
14
96
};
15
97
} // namespace Test
0 commit comments