Skip to content

Commit 553a579

Browse files
committed
Change quaternion tests to not use ub
1 parent fcb8cc9 commit 553a579

File tree

4 files changed

+17
-24
lines changed

4 files changed

+17
-24
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,16 @@ As well as a short example of its usage:
219219
```cpp
220220
struct quaternion
221221
{
222-
float w;
223-
float x;
224-
float y;
225-
float z;
226-
227-
// This needs to return the same order as the constructor
222+
// smallest_three supports any combination of w, x, y and z, as long as it's consistent
223+
float values[4];
224+
225+
// The constructor order must be the same as the operator[]
228226
float operator[](size_t index) const
229227
{
230-
return reinterpret_cast<const float*>(this)[index];
228+
return values[index];
231229
}
232230
};
233-
quaternion in_value = { 1.0f, 0.0f, 0.0f, 0.0f };
231+
quaternion in_value{ 1.0f, 0.0f, 0.0f, 0.0f };
234232
quaternion out_value;
235233
bool status_write = writer.serialize<smallest_three<quaternion, 12>>(in_value);
236234
bool status_read = reader.serialize<smallest_three<quaternion, 12>>(out_value);

src/shared/test_types.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
#pragma once
22

3+
#include <bitstream/utility/assert.h>
4+
35
#include <cstddef>
46

57
namespace bitstream::test
68
{
7-
struct quaternion
9+
struct quaternion
810
{
911
// smallest_three supports any combination of w, x, y and z, as long as it's consistent
10-
float w;
11-
float x;
12-
float y;
13-
float z;
14-
15-
quaternion() = default;
12+
float values[4];
1613

1714
// The constructor order must be the same as the operator[]
18-
quaternion(float w, float x, float y, float z)
19-
: w(w), x(x), y(y), z(z) {}
20-
21-
// smallest_three uses this operator
2215
float operator[](size_t index) const
2316
{
24-
return reinterpret_cast<const float*>(this)[index];
17+
BS_ASSERT(index >= 0 && index < 4);
18+
19+
return values[index];
2520
}
2621
};
2722
}

src/test/quantization_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ namespace bitstream::test::quantization
4040

4141
BS_ADD_TEST(test_smallest_three)
4242
{
43-
quaternion quat_in(0.0f, std::sin(2.0f), std::cos(2.0f), 0.0f);
43+
quaternion quat_in{ 0.0f, std::sin(2.0f), std::cos(2.0f), 0.0f };
4444

4545
auto quantized_quat = smallest_three<quaternion, 11>::quantize(quat_in);
4646
quaternion quat_out = smallest_three<quaternion, 11>::dequantize(quantized_quat);
4747

48-
float dot = quat_in.x * quat_out.x + quat_in.y * quat_out.y + quat_in.z * quat_out.z + quat_in.w * quat_out.w;
48+
float dot = quat_in[0] * quat_out[0] + quat_in[1] * quat_out[1] + quat_in[2] * quat_out[2] + quat_in[3] * quat_out[3];
4949

5050
if (dot < 0.0f)
5151
dot *= -1.0f;

src/test/serialize_quantization_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace bitstream::test::traits
8686
using trait = smallest_three<quaternion, 11>;
8787

8888
// Test smallest three
89-
quaternion value_in(0.0f, std::sin(2.0f), std::cos(2.0f), 0.0f);
89+
quaternion value_in{ 0.0f, std::sin(2.0f), std::cos(2.0f), 0.0f };
9090

9191
byte_buffer<16> buffer;
9292
bit_writer writer(buffer);
@@ -102,7 +102,7 @@ namespace bitstream::test::traits
102102

103103
BS_TEST_ASSERT(reader.serialize<trait>(value_out));
104104

105-
float dot = value_in.x * value_out.x + value_in.y * value_out.y + value_in.z * value_out.z + value_in.w * value_out.w;
105+
float dot = value_in[0] * value_out[0] + value_in[1] * value_out[1] + value_in[2] * value_out[2] + value_in[3] * value_out[3];
106106

107107
if (dot < 0.0f)
108108
dot *= -1.0f;

0 commit comments

Comments
 (0)