git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Test code for htonll, ntohll
@ 2013-10-31 13:26 Torsten Bögershausen
  0 siblings, 0 replies; only message in thread
From: Torsten Bögershausen @ 2013-10-31 13:26 UTC (permalink / raw)
  To: git; +Cc: ramsay, tanoku


test-endianess.c adds test code for htonl(), ntohll()
and the recently introduced ntohll() and htonll()

The test is called in t0070

Signed-off-by: Torsten Bögershausen <tboegi@web.de>
---
 .gitignore             |    1 +
 Makefile               |    3 +++
 t/t0070-fundamental.sh |    3 +++
 test-endianess.c       |   58 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 65 insertions(+)
 create mode 100644 test-endianess.c

diff --git a/.gitignore b/.gitignore
index 66199ed..750e7d8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -183,6 +183,7 @@
 /test-date
 /test-delta
 /test-dump-cache-tree
+/test-endianess
 /test-scrap-cache-tree
 /test-genrandom
 /test-index-version
diff --git a/Makefile b/Makefile
index 07b0626..50957c7 100644
--- a/Makefile
+++ b/Makefile
@@ -555,6 +555,7 @@ TEST_PROGRAMS_NEED_X += test-ctype
 TEST_PROGRAMS_NEED_X += test-date
 TEST_PROGRAMS_NEED_X += test-delta
 TEST_PROGRAMS_NEED_X += test-dump-cache-tree
+TEST_PROGRAMS_NEED_X += test-endianess
 TEST_PROGRAMS_NEED_X += test-genrandom
 TEST_PROGRAMS_NEED_X += test-hashmap
 TEST_PROGRAMS_NEED_X += test-index-version
@@ -2275,6 +2276,8 @@ test-date$X: date.o ctype.o
 
 test-delta$X: diff-delta.o patch-delta.o
 
+test-endianess$X: test-endianess.c
+
 test-line-buffer$X: vcs-svn/lib.a
 
 test-parse-options$X: parse-options.o parse-options-cb.o
diff --git a/t/t0070-fundamental.sh b/t/t0070-fundamental.sh
index 5ed69a6..dc687da 100755
--- a/t/t0070-fundamental.sh
+++ b/t/t0070-fundamental.sh
@@ -33,5 +33,8 @@ test_expect_success 'check for a bug in the regex routines' '
 	# if this test fails, re-build git with NO_REGEX=1
 	test-regex
 '
+test_expect_success 'test endianess, htonll(), ntohll()' '
+	test-endianess
+'
 
 test_done
diff --git a/test-endianess.c b/test-endianess.c
new file mode 100644
index 0000000..5b49175
--- /dev/null
+++ b/test-endianess.c
@@ -0,0 +1,58 @@
+#include "cache.h"
+
+int main(int argc, char **argv)
+{
+	union {
+		uint8_t  bytes[8];
+		uint32_t word32;
+		uint64_t word64;
+	} wordll;
+	volatile uint64_t word64;
+	volatile uint32_t word32;
+
+	wordll.bytes[0] = 0x80;
+	wordll.bytes[1] = 0x81;
+	wordll.bytes[2] = 0x82;
+	wordll.bytes[3] = 0x83;
+	wordll.bytes[4] = 0x00;
+	wordll.bytes[5] = 0x01;
+	wordll.bytes[6] = 0x02;
+	wordll.bytes[7] = 0x03;
+
+	word32 = htonl(wordll.word32);
+	if (word32 != 0x80818283)
+		die("htonl word32 != 0x80818283");
+
+	word64 = htonll(wordll.word64);
+	if (word64 != 0x8081828300010203ULL)
+		die("htonll word64 != 0x8081828300010203");
+
+	word32 = ntohl(wordll.word32);
+	if (word32 != 0x80818283)
+		die("ntohl word32 != 0x80818283");
+
+	word64 = ntohll(wordll.word64);
+	if (word64 != 0x8081828300010203ULL)
+		die("ntohll word64 != 0x8081828300010203");
+
+	wordll.bytes[0] = 0x04;
+	wordll.bytes[4] = 0x84;
+
+	word32 = htonl(wordll.word32);
+	if (word32 != 0x04818283)
+		die("htonl word32 != 0x04818283");
+
+	word64 = htonll(wordll.word64);
+	if (word64 != 0x0481828384010203ULL)
+		die("htonll word64 != 0x0481828384010203");
+
+	word32 = ntohl(wordll.word32);
+	if (word32 != 0x04818283)
+		die("ntohl word32 != 0x04818283");
+
+	word64 = ntohll(wordll.word64);
+	if (word64 != 0x0481828384010203ULL)
+		die("ntohll word64 != 0x0481828384010203");
+
+	return 0;
+}
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2013-10-31 13:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-10-31 13:26 [PATCH] Test code for htonll, ntohll Torsten Bögershausen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).