Skip to content

Commit 815f414

Browse files
committed
Uses built-in byte swap if available (Apple) and if bswap_XX is undefined.
Defers to pre-defined version if found (e.g. protobuf). For protobuf case, the definitions are identical and thus include order should not affect results.
1 parent c9e0059 commit 815f414

File tree

7 files changed

+95
-0
lines changed

7 files changed

+95
-0
lines changed

src/Makefile.qttest.include

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ bin_PROGRAMS += qt/test/test_bitcoin-qt
66
TESTS += qt/test/test_bitcoin-qt
77

88
TEST_QT_MOC_CPP = \
9+
qt/test/moc_compattests.cpp \
910
qt/test/moc_rpcnestedtests.cpp \
1011
qt/test/moc_uritests.cpp
1112

@@ -14,6 +15,7 @@ TEST_QT_MOC_CPP += qt/test/moc_paymentservertests.cpp
1415
endif
1516

1617
TEST_QT_H = \
18+
qt/test/compattests.h \
1719
qt/test/rpcnestedtests.h \
1820
qt/test/uritests.h \
1921
qt/test/paymentrequestdata.h \
@@ -23,6 +25,7 @@ qt_test_test_bitcoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_
2325
$(QT_INCLUDES) $(QT_TEST_INCLUDES) $(PROTOBUF_CFLAGS)
2426

2527
qt_test_test_bitcoin_qt_SOURCES = \
28+
qt/test/compattests.cpp \
2629
qt/test/rpcnestedtests.cpp \
2730
qt/test/test_main.cpp \
2831
qt/test/uritests.cpp \

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ BITCOIN_TESTS =\
5252
test/bip32_tests.cpp \
5353
test/blockencodings_tests.cpp \
5454
test/bloom_tests.cpp \
55+
test/bswap_tests.cpp \
5556
test/coins_tests.cpp \
5657
test/compress_tests.cpp \
5758
test/crypto_tests.cpp \

src/compat/byteswap.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@
1515
#include <byteswap.h>
1616
#endif
1717

18+
#if defined(__APPLE__)
19+
20+
#if !defined(bswap_16)
21+
22+
// Mac OS X / Darwin features; we include a check for bswap_16 because if it is already defined, protobuf has
23+
// defined these macros for us already; if it isn't, we do it ourselves. In either case, we get the exact same
24+
// result regardless which path was taken
25+
#include <libkern/OSByteOrder.h>
26+
#define bswap_16(x) OSSwapInt16(x)
27+
#define bswap_32(x) OSSwapInt32(x)
28+
#define bswap_64(x) OSSwapInt64(x)
29+
30+
#endif // !defined(bswap_16)
31+
32+
#else
33+
// Non-Mac OS X / non-Darwin
34+
1835
#if HAVE_DECL_BSWAP_16 == 0
1936
inline uint16_t bswap_16(uint16_t x)
2037
{
@@ -44,4 +61,6 @@ inline uint64_t bswap_64(uint64_t x)
4461
}
4562
#endif // HAVE_DECL_BSWAP64
4663

64+
#endif // defined(__APPLE__)
65+
4766
#endif // BITCOIN_COMPAT_BYTESWAP_H

src/qt/test/compattests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2016 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "paymentrequestplus.h" // this includes protobuf's port.h which defines its own bswap macos
6+
7+
#include "compattests.h"
8+
9+
#include "compat/byteswap.h"
10+
11+
void CompatTests::bswapTests()
12+
{
13+
// Sibling in bitcoin/src/test/bswap_tests.cpp
14+
uint16_t u1 = 0x1234;
15+
uint32_t u2 = 0x56789abc;
16+
uint64_t u3 = 0xdef0123456789abc;
17+
uint16_t e1 = 0x3412;
18+
uint32_t e2 = 0xbc9a7856;
19+
uint64_t e3 = 0xbc9a78563412f0de;
20+
QVERIFY(bswap_16(u1) == e1);
21+
QVERIFY(bswap_32(u2) == e2);
22+
QVERIFY(bswap_64(u3) == e3);
23+
}

src/qt/test/compattests.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2009-2015 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_QT_TEST_COMPATTESTS_H
6+
#define BITCOIN_QT_TEST_COMPATTESTS_H
7+
8+
#include <QObject>
9+
#include <QTest>
10+
11+
class CompatTests : public QObject
12+
{
13+
Q_OBJECT
14+
15+
private Q_SLOTS:
16+
void bswapTests();
17+
};
18+
19+
#endif // BITCOIN_QT_TEST_COMPATTESTS_H

src/qt/test/test_main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "rpcnestedtests.h"
1212
#include "util.h"
1313
#include "uritests.h"
14+
#include "compattests.h"
1415

1516
#ifdef ENABLE_WALLET
1617
#include "paymentservertests.h"
@@ -61,6 +62,9 @@ int main(int argc, char *argv[])
6162
RPCNestedTests test3;
6263
if (QTest::qExec(&test3) != 0)
6364
fInvalid = true;
65+
CompatTests test4;
66+
if (QTest::qExec(&test4) != 0)
67+
fInvalid = true;
6468

6569
ECC_Stop();
6670
return fInvalid;

src/test/bswap_tests.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2016 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "compat/byteswap.h"
6+
#include "test/test_bitcoin.h"
7+
8+
#include <boost/test/unit_test.hpp>
9+
10+
BOOST_FIXTURE_TEST_SUITE(bswap_tests, BasicTestingSetup)
11+
12+
BOOST_AUTO_TEST_CASE(bswap_tests)
13+
{
14+
// Sibling in bitcoin/src/qt/test/compattests.cpp
15+
uint16_t u1 = 0x1234;
16+
uint32_t u2 = 0x56789abc;
17+
uint64_t u3 = 0xdef0123456789abc;
18+
uint16_t e1 = 0x3412;
19+
uint32_t e2 = 0xbc9a7856;
20+
uint64_t e3 = 0xbc9a78563412f0de;
21+
BOOST_CHECK(bswap_16(u1) == e1);
22+
BOOST_CHECK(bswap_32(u2) == e2);
23+
BOOST_CHECK(bswap_64(u3) == e3);
24+
}
25+
26+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)