Skip to content

Commit 57bf806

Browse files
committed
r28 | charles.nicholson | 2010-07-29 17:43:45 -0500 (Thu, 29 Jul 2010) | 1 line
fix strict aliasing violation, was causing gcc failures. use std::memcpy instead of union-cast
1 parent f5ba405 commit 57bf806

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/tests/TestChecks.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "../../unittestpp.h"
22
#include "RecordingReporter.h"
33

4+
#include <cstring>
5+
46
using namespace UnitTest;
57

68

@@ -119,25 +121,21 @@ TEST(CheckCloseWithZeroEpsilonWorksForSameNumber)
119121

120122
TEST(CheckCloseWithNaNFails)
121123
{
122-
union
123-
{
124-
unsigned int bitpattern;
125-
float nan;
126-
};
127-
bitpattern = 0xFFFFFFFF;
128-
TestResults results;
124+
const unsigned int bitpattern = 0xFFFFFFFF;
125+
float nan;
126+
std::memcpy(&nan, &bitpattern, sizeof(bitpattern));
127+
128+
TestResults results;
129129
CheckClose(results, 3.0f, nan, 0.1f, TestDetails("", "", "", 0));
130130
CHECK_EQUAL(1, results.GetFailureCount());
131131
}
132132

133133
TEST(CheckCloseWithNaNAgainstItselfFails)
134134
{
135-
union
136-
{
137-
unsigned int bitpattern;
138-
float nan;
139-
};
140-
bitpattern = 0xFFFFFFFF;
135+
const unsigned int bitpattern = 0xFFFFFFFF;
136+
float nan;
137+
std::memcpy(&nan, &bitpattern, sizeof(bitpattern));
138+
141139
TestResults results;
142140
CheckClose(results, nan, nan, 0.1f, TestDetails("", "", "", 0));
143141
CHECK_EQUAL(1, results.GetFailureCount());

0 commit comments

Comments
 (0)