summaryrefslogtreecommitdiffstats
path: root/tests/endian
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-05-15 12:07:51 +0200
committerHarald Welte <laforge@gnumonks.org>2017-05-15 13:42:00 +0200
commit3318c657dea52c143842dab0e0f7733288f7bd4d (patch)
treeef025d5907529f7d4f4614e6d110e54b9ca0e045 /tests/endian
parent054667fe15434e7e467311c7213b50f849aecc79 (diff)
introduce byteswap.h with osmo_{htonl,ntohl,htons,ntohs}
We need to have an architecture-independend way of endian conversion / byte swapping functions which will also work on embedded (bare iron) builds. Let's introduce osmocom/core/bytesawp.h for this purpose. Change-Id: Ibc0cc1e36d4ed63a35cf8ceff3af0f26e5ac7a3d
Diffstat (limited to 'tests/endian')
-rw-r--r--tests/endian/endian_test.c31
-rw-r--r--tests/endian/endian_test.ok3
2 files changed, 34 insertions, 0 deletions
diff --git a/tests/endian/endian_test.c b/tests/endian/endian_test.c
new file mode 100644
index 00000000..8737e7a4
--- /dev/null
+++ b/tests/endian/endian_test.c
@@ -0,0 +1,31 @@
+#include <osmocom/core/byteswap.h>
+#include <osmocom/core/utils.h>
+
+int main(int argc, char **argv)
+{
+ printf("Testing 16bit swappinng\n");
+ OSMO_ASSERT(osmo_swab16(0x1234) == 0x3412);
+ printf("Testing 32bit swappinng\n");
+ OSMO_ASSERT(osmo_swab32(0x12345678) == 0x78563412);
+
+ printf("Testing ntohX() and htonX()\n");
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#if OSMO_IS_LITTLE_ENDIAN == 0
+#error "Something wrong with endianness detection!"
+#endif /* IS_LITTLE_ENDIAN */
+ OSMO_ASSERT(osmo_ntohs(0x1234) == 0x3412);
+ OSMO_ASSERT(osmo_htons(0x1234) == 0x3412);
+ OSMO_ASSERT(osmo_htonl(0x12345678) == 0x78563412);
+ OSMO_ASSERT(osmo_ntohl(0x12345678) == 0x78563412);
+#else
+#if OSMO_IS_LITTLE_ENDIAN == 1
+#error "Something wrong with endianness detection!"
+#endif /* IS_LITTLE_ENDIAN */
+ OSMO_ASSERT(osmo_ntohs(0x1234) == 0x1234);
+ OSMO_ASSERT(osmo_htons(0x1234) == 0x1234);
+ OSMO_ASSERT(osmo_htonl(0x12345678) == 0x12345678);
+ OSMO_ASSERT(osmo_ntohl(0x12345678) == 0x12345678);
+#endif /* __BYTE_ORDER__ */
+
+ exit(0);
+}
diff --git a/tests/endian/endian_test.ok b/tests/endian/endian_test.ok
new file mode 100644
index 00000000..a42f4575
--- /dev/null
+++ b/tests/endian/endian_test.ok
@@ -0,0 +1,3 @@
+Testing 16bit swappinng
+Testing 32bit swappinng
+Testing ntohX() and htonX()