libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2021-01-26 00:11:04
Commitb9d4a23d82423addb83cd3704c414e3665b7bdde
Parent9f6b39c028bf2eb00657f7626bead56ece55d387

endian: Add endian unit test

Diffstat

M CMakeLists.txt | 1 +
A src/test_endian.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M src/unit.h | 16 ++++++++++++++++

3 files changed, 101 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 409ecd6..e7cf41d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,5 +14,6 @@ ADD_LIBRARY(lib STATIC ${LIBSRC})
 
 LINK_LIBRARIES(lib)
 
+ADD_EXECUTABLE(test_endian ${PROJECT_SOURCE_DIR}/src/test_endian.c)
 ADD_EXECUTABLE(test_error  ${PROJECT_SOURCE_DIR}/src/test_error.c)
 ADD_EXECUTABLE(test_optget ${PROJECT_SOURCE_DIR}/src/test_optget.c)
diff --git a/src/test_endian.c b/src/test_endian.c
new file mode 100644
index 0000000..bba612d
--- /dev/null
+++ b/src/test_endian.c
@@ -0,0 +1,84 @@
+// test_endian.c
+// Endian unit test for OMKOV lib
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Public Domain Licence, version 1.0
+
+Permission is hereby granted to deal with this software and its associated
+documentation files without restriction.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+#include "lib/endian.h"
+#include "unit.h"
+
+#include <stdint.h>
+#include <stdio.h>
+
+int main(void) {
+	{
+		ASSERT("G00T00 BSWAP16",
+				BSWAP16(0xFF00u) == 0x00FFu);
+		ASSERT("G00T02 BSWAP32",
+				BSWAP32(0xFF00FF00u) == 0x00FF00FFu);
+		ASSERT("G00T04 BSWAP64",
+				BSWAP64(0xFF00FF00FF00FF00) == 0x00FF00FF00FF00FF);
+	}
+	
+	{
+		uint8_t a16[2] = { 0xFF, 0x00 };
+		uint8_t a32[4] = { 0xFF, 0x00, 0x00, 0x00 };
+		uint8_t a64[8] = { 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+		
+		ASSERT("G01T00 LD16LE",
+				LD16LE(a16) == 0x00FFu);
+		ASSERT("G01T01 LD16BE",
+				LD16BE(a16) == 0xFF00u);
+		ASSERT("G01T02 LD32LE",
+				LD32LE(a32) == 0x000000FFu);
+		ASSERT("G01T03 LD32BE",
+				LD32BE(a32) == 0xFF000000u);
+		ASSERT("G01T04 LD64LE",
+				LD64LE(a64) == 0x00000000000000FFu);
+		ASSERT("G01T05 LD64BE",
+				LD64BE(a64) == 0xFF00000000000000u);
+	}
+	
+	{
+		uint16_t a16 = 0x00FFu; uint32_t a32 = 0x000000FFu;
+		uint64_t a64 = 0x00000000000000FFu;
+		uint8_t b16[2]; uint8_t b32[4]; uint8_t b64[8];
+		
+		ST16LE(b16, a16); ASSERT("G02T00 ST16LE",
+				b16[0] == 0xFFu && b16[1] == 0x00u);
+		ST16BE(b16, a16); ASSERT("G02T01 ST16BE",
+				b16[0] == 0x00u && b16[1] == 0xFFu);
+		ST32LE(b32, a32); ASSERT("G02T02 ST32LE",
+				b32[0] == 0xFFu && b32[1] == 0x00u &&
+				b32[2] == 0x00u && b32[3] == 0x00u);
+		ST32BE(b32, a32); ASSERT("G02T03 ST32BE",
+				b32[0] == 0x00u && b32[1] == 0x00u &&
+				b32[2] == 0x00u && b32[3] == 0xFFu);
+		ST64LE(b64, a64); ASSERT("G02T04 ST64LE",
+				b64[0] == 0xFFu && b64[1] == 0x00u &&
+				b64[2] == 0x00u && b64[3] == 0x00u &&
+				b64[4] == 0x00u && b64[5] == 0x00u &&
+				b64[6] == 0x00u && b64[7] == 0x00u);
+		ST64BE(b64, a64); ASSERT("G02T05 ST64BE",
+				b64[0] == 0x00u && b64[1] == 0x00u &&
+				b64[2] == 0x00u && b64[3] == 0x00u &&
+				b64[4] == 0x00u && b64[5] == 0x00u &&
+				b64[6] == 0x00u && b64[7] == 0xFFu);
+	}
+	
+	printf("%d of %d tests passed\n", testspassed, testsrun);
+	return testsfailed;
+}
diff --git a/src/unit.h b/src/unit.h
index b42e184..fdfe25a 100644
--- a/src/unit.h
+++ b/src/unit.h
@@ -3,9 +3,25 @@
 // Copyright (C) 2019, Jakob Wakeling
 // All righs reserved.
 
+/*
+OMKOV Public Domain Licence, version 1.0
+
+Permission is hereby granted to deal with this software and its associated
+documentation files without restriction.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
 #ifndef OMKOV_UNIT_H_YNUUN45Z
 #define OMKOV_UNIT_H_YNUUN45Z
 
+#include <stdio.h>
+
 #define PASS(message) printf("\033[1;32mPASS\033[0m %s\n", message)
 #define FAIL(message) printf("\033[1;31mFAIL\033[0m %s\n", message)