Skip to content

Commit 59c37ae

Browse files
kallewooflaanwj
authored andcommitted
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. Github-Pull: #9366 Rebased-From: 815f414
1 parent 77eaadb commit 59c37ae

File tree

7 files changed

+97
-1
lines changed

7 files changed

+97
-1
lines changed

src/Makefile.qttest.include

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
bin_PROGRAMS += qt/test/test_bitcoin-qt
66
TESTS += qt/test/test_bitcoin-qt
77

8-
TEST_QT_MOC_CPP = qt/test/moc_uritests.cpp
8+
TEST_QT_MOC_CPP = \
9+
qt/test/moc_compattests.cpp \
10+
qt/test/moc_uritests.cpp
911

1012
if ENABLE_WALLET
1113
TEST_QT_MOC_CPP += qt/test/moc_paymentservertests.cpp
1214
endif
1315

1416
TEST_QT_H = \
17+
qt/test/compattests.h \
1518
qt/test/uritests.h \
1619
qt/test/paymentrequestdata.h \
1720
qt/test/paymentservertests.h
@@ -20,6 +23,7 @@ qt_test_test_bitcoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_
2023
$(QT_INCLUDES) $(QT_TEST_INCLUDES) $(PROTOBUF_CFLAGS)
2124

2225
qt_test_test_bitcoin_qt_SOURCES = \
26+
qt/test/compattests.cpp \
2327
qt/test/test_main.cpp \
2428
qt/test/uritests.cpp \
2529
$(TEST_QT_H)

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ BITCOIN_TESTS =\
5050
test/bip32_tests.cpp \
5151
test/blockencodings_tests.cpp \
5252
test/bloom_tests.cpp \
53+
test/bswap_tests.cpp \
5354
test/coins_tests.cpp \
5455
test/compress_tests.cpp \
5556
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
@@ -8,6 +8,7 @@
88

99
#include "util.h"
1010
#include "uritests.h"
11+
#include "compattests.h"
1112

1213
#ifdef ENABLE_WALLET
1314
#include "paymentservertests.h"
@@ -48,6 +49,9 @@ int main(int argc, char *argv[])
4849
if (QTest::qExec(&test2) != 0)
4950
fInvalid = true;
5051
#endif
52+
CompatTests test4;
53+
if (QTest::qExec(&test4) != 0)
54+
fInvalid = true;
5155

5256
return fInvalid;
5357
}

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)