libutil

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

AuthorJamozed <[email protected]>
Date2021-02-21 10:23:53
Commit4b7a4486ddf5e17e5740d8a9e99a9ed4f4d9c2e0
Parent99202e8d665be591e9f50fa3e332206df0dbf58f

strconv: Add strconv unit tests

Diffstat

M CMakeLists.txt | 26 ++++++++++++++------------
A src/test/test_strconv.c | 366 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 380 insertions, 12 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 88397a7..8fc5dcd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -11,17 +11,19 @@ ADD_LIBRARY(lib STATIC ${SOURCES})
 
 LINK_LIBRARIES(lib)
 
-ADD_EXECUTABLE(test_base32 ${PROJECT_SOURCE_DIR}/src/test/test_base32.c)
-ADD_EXECUTABLE(test_base64 ${PROJECT_SOURCE_DIR}/src/test/test_base64.c)
-ADD_EXECUTABLE(test_crypt  ${PROJECT_SOURCE_DIR}/src/test/test_crypt.c)
-ADD_EXECUTABLE(test_endian ${PROJECT_SOURCE_DIR}/src/test/test_endian.c)
-ADD_EXECUTABLE(test_error  ${PROJECT_SOURCE_DIR}/src/test/test_error.c)
-ADD_EXECUTABLE(test_optget ${PROJECT_SOURCE_DIR}/src/test/test_optget.c)
+ADD_EXECUTABLE(test_base32  ${PROJECT_SOURCE_DIR}/src/test/test_base32.c)
+ADD_EXECUTABLE(test_base64  ${PROJECT_SOURCE_DIR}/src/test/test_base64.c)
+ADD_EXECUTABLE(test_crypt   ${PROJECT_SOURCE_DIR}/src/test/test_crypt.c)
+ADD_EXECUTABLE(test_endian  ${PROJECT_SOURCE_DIR}/src/test/test_endian.c)
+ADD_EXECUTABLE(test_error   ${PROJECT_SOURCE_DIR}/src/test/test_error.c)
+ADD_EXECUTABLE(test_optget  ${PROJECT_SOURCE_DIR}/src/test/test_optget.c)
+ADD_EXECUTABLE(test_strconv ${PROJECT_SOURCE_DIR}/src/test/test_strconv.c)
 
 ENABLE_TESTING()
-ADD_TEST(NAME test_base32 COMMAND test_base32)
-ADD_TEST(NAME test_base64 COMMAND test_base64)
-ADD_TEST(NAME test_crypt  COMMAND test_crypt)
-ADD_TEST(NAME test_endian COMMAND test_endian)
-ADD_TEST(NAME test_error  COMMAND test_error)
-ADD_TEST(NAME test_optget COMMAND test_optget)
+ADD_TEST(NAME test_base32  COMMAND test_base32)
+ADD_TEST(NAME test_base64  COMMAND test_base64)
+ADD_TEST(NAME test_crypt   COMMAND test_crypt)
+ADD_TEST(NAME test_endian  COMMAND test_endian)
+ADD_TEST(NAME test_error   COMMAND test_error)
+ADD_TEST(NAME test_optget  COMMAND test_optget)
+ADD_TEST(NAME test_strconv COMMAND test_strconv)
diff --git a/src/test/test_strconv.c b/src/test/test_strconv.c
new file mode 100644
index 0000000..83d9781
--- /dev/null
+++ b/src/test/test_strconv.c
@@ -0,0 +1,366 @@
+// test_strconv.c
+// String conversion 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 "../strconv.h"
+#include "unit.h"
+
+#include <stdint.h>
+#include <stdio.h>
+#include <errno.h>
+
+int main(void) {
+	/* STRTOU8 */
+	{
+		char i[] = "0";
+		
+		ASSERT("T000 STRTOU8 ZERO BASE 0",
+				strtou8(i, NULL, 0) == 0);
+		
+		ASSERT("T001 STRTOU8 ZERO BASE 8",
+				strtou8(i, NULL, 8) == 0);
+	}
+	
+	{
+		char i[] = "64";
+		
+		ASSERT("T002 STRTOU8 NORMAL BASE 0",
+				strtou8(i, NULL, 0) == 64);
+		
+		ASSERT("T003 STRTOU8 NORMAL BASE 8",
+				strtou8(i, NULL, 8) == 52);
+		
+		ASSERT("T004 STRTOU8 NORMAL BASE 10",
+				strtou8(i, NULL, 10) == 64);
+		
+		ASSERT("T005 STRTOU8 NORMAL BASE 16",
+				strtou8(i, NULL, 16) == 100);
+	}
+	
+	{
+		char i[] = "3M";
+		
+		ASSERT("T006 STRTOU8 NORMAL BASE 36",
+				strtou8(i, NULL, 36) == 130u);
+	}
+	
+	{
+		char i8[] = "0377";
+		char i10[] = "255";
+		char i16[] = "0xFF";
+		
+		errno = 0;
+		ASSERT("T007 STRTOU8 MAX BASE 0",
+				strtou8(i8, NULL, 0) == UINT8_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T008 STRTOU8 MAX BASE 8",
+				strtou8(i8, NULL, 8) == UINT8_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T009 STRTOU8 MAX BASE 10",
+				strtou8(i10, NULL, 10) == UINT8_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T010 STRTOU8 MAX BASE 16",
+				strtou8(i16, NULL, 16) == UINT8_MAX && errno != ERANGE);
+	}
+	
+	{
+		char i[] = "-153";
+		
+		errno = 0;
+		ASSERT("T011 STRTOU8 NEGATIVE",
+				!strtou8(i, NULL, 10) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T012 STRTOU8 BASE 1",
+				!strtou8(i, NULL, 1) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T013 STRTOU8 BASE -1",
+				!strtou8(i, NULL, -1) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T014 STRTOU8 BASE 37",
+				!strtou8(i, NULL, 37) && errno == EINVAL);
+	}
+	
+	{
+		char i[] = "257";
+		
+		errno = 0;
+		ASSERT("T015 STRTOU8 OVERFLOW",
+				strtou8(i, NULL, 10) == UINT8_MAX && errno == ERANGE);
+	}
+	
+	/* STRTOU16 */
+	{
+		char i[] = "0";
+		
+		ASSERT("T100 STRTOU16 ZERO BASE 0",
+				strtou16(i, NULL, 0) == 0);
+		
+		ASSERT("T101 STRTOU16 ZERO BASE 8",
+				strtou16(i, NULL, 8) == 0);
+	}
+	
+	{
+		char i[] = "64";
+		
+		ASSERT("T102 STRTOU16 NORMAL BASE 0",
+				strtou16(i, NULL, 0) == 64);
+		
+		ASSERT("T103 STRTOU16 NORMAL BASE 8",
+				strtou16(i, NULL, 8) == 52);
+		
+		ASSERT("T104 STRTOU16 NORMAL BASE 10",
+				strtou16(i, NULL, 10) == 64);
+		
+		ASSERT("T105 STRTOU16 NORMAL BASE 16",
+				strtou16(i, NULL, 16) == 100);
+	}
+	
+	{
+		char i[] = "AZ1";
+		
+		ASSERT("T106 STRTOU16 NORMAL BASE 36",
+				strtou16(i, NULL, 36) == 14221u);
+	}
+	
+	{
+		char i8[] = "0177777";
+		char i10[] = "65535";
+		char i16[] = "0xFFFF";
+		
+		errno = 0;
+		ASSERT("T107 STRTOU16 MAX BASE 0",
+				strtou16(i8, NULL, 0) == UINT16_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T108 STRTOU16 MAX BASE 8",
+				strtou16(i8, NULL, 8) == UINT16_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T109 STRTOU16 MAX BASE 10",
+				strtou16(i10, NULL, 10) == UINT16_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T110 STRTOU16 MAX BASE 16",
+				strtou16(i16, NULL, 16) == UINT16_MAX && errno != ERANGE);
+	}
+	
+	{
+		char i[] = "-153";
+		
+		errno = 0;
+		ASSERT("T111 STRTOU16 NEGATIVE",
+				!strtou16(i, NULL, 10) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T112 STRTOU16 BASE 1",
+				!strtou16(i, NULL, 1) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T113 STRTOU16 BASE -1",
+				!strtou16(i, NULL, -1) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T114 STRTOU16 BASE 37",
+				!strtou16(i, NULL, 37) && errno == EINVAL);
+	}
+	
+	{
+		char i[] = "65537";
+		
+		errno = 0;
+		ASSERT("T115 STRTOU16 OVERFLOW",
+				strtou16(i, NULL, 10) == UINT16_MAX && errno == ERANGE);
+	}
+	
+	/* STRTOU32 */
+	{
+		char i[] = "0";
+		
+		ASSERT("T200 STRTOU32 ZERO BASE 0",
+				strtou32(i, NULL, 0) == 0);
+		
+		ASSERT("T201 STRTOU32 ZERO BASE 8",
+				strtou32(i, NULL, 8) == 0);
+	}
+	
+	{
+		char i[] = "64";
+		
+		ASSERT("T202 STRTOU32 NORMAL BASE 0",
+				strtou32(i, NULL, 0) == 64);
+		
+		ASSERT("T203 STRTOU32 NORMAL BASE 8",
+				strtou32(i, NULL, 8) == 52);
+		
+		ASSERT("T204 STRTOU32 NORMAL BASE 10",
+				strtou32(i, NULL, 10) == 64);
+		
+		ASSERT("T205 STRTOU32 NORMAL BASE 16",
+				strtou32(i, NULL, 16) == 100);
+	}
+	
+	{
+		char i[] = "T7A7J";
+		
+		ASSERT("T206 STRTOU32 NORMAL BASE 36",
+				strtou32(i, NULL, 36) == 49048687u);
+	}
+	
+	{
+		char i8[] = "037777777777";
+		char i10[] = "4294967295";
+		char i16[] = "0xFFFFFFFF";
+		
+		errno = 0;
+		ASSERT("T207 STRTOU32 MAX BASE 0",
+				strtou32(i8, NULL, 0) == UINT32_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T208 STRTOU32 MAX BASE 8",
+				strtou32(i8, NULL, 8) == UINT32_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T209 STRTOU32 MAX BASE 10",
+				strtou32(i10, NULL, 10) == UINT32_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T210 STRTOU32 MAX BASE 16",
+				strtou32(i16, NULL, 16) == UINT32_MAX && errno != ERANGE);
+	}
+	
+	{
+		char i[] = "-153";
+		
+		errno = 0;
+		ASSERT("T211 STRTOU32 NEGATIVE",
+				!strtou32(i, NULL, 10) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T212 STRTOU32 BASE 1",
+				!strtou32(i, NULL, 1) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T213 STRTOU32 BASE -1",
+				!strtou32(i, NULL, -1) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T214 STRTOU32 BASE 37",
+				!strtou32(i, NULL, 37) && errno == EINVAL);
+	}
+	
+	{
+		char i[] = "4294967297";
+		
+		errno = 0;
+		ASSERT("T215 STRTOU32 OVERFLOW",
+				strtou32(i, NULL, 10) == UINT32_MAX && errno == ERANGE);
+	}
+	
+	/* STRTOU64 */
+	{
+		char i[] = "0";
+		
+		ASSERT("T300 STRTOU64 ZERO BASE 0",
+				strtou64(i, NULL, 0) == 0);
+		
+		ASSERT("T301 STRTOU64 ZERO BASE 8",
+				strtou64(i, NULL, 8) == 0);
+	}
+	
+	{
+		char i[] = "64";
+		
+		ASSERT("T302 STRTOU64 NORMAL BASE 0",
+				strtou64(i, NULL, 0) == 64);
+		
+		ASSERT("T303 STRTOU64 NORMAL BASE 8",
+				strtou64(i, NULL, 8) == 52);
+		
+		ASSERT("T304 STRTOU64 NORMAL BASE 10",
+				strtou64(i, NULL, 10) == 64);
+		
+		ASSERT("T305 STRTOU64 NORMAL BASE 16",
+				strtou64(i, NULL, 16) == 100);
+	}
+	
+	{
+		char i[] = "3579ADGJMPSVZ";
+		
+		ASSERT("T306 STRTOU64 NORMAL BASE 36",
+				strtou64(i, NULL, 36) == 14899788948715380863u);
+	}
+	
+	{
+		char i8[] = "01777777777777777777777";
+		char i10[] = "18446744073709551615";
+		char i16[] = "0xFFFFFFFFFFFFFFFF";
+		
+		errno = 0;
+		ASSERT("T307 STRTOU64 MAX BASE 0",
+				strtou64(i8, NULL, 0) == UINT64_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T308 STRTOU64 MAX BASE 8",
+				strtou64(i8, NULL, 8) == UINT64_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T309 STRTOU64 MAX BASE 10",
+				strtou64(i10, NULL, 10) == UINT64_MAX && errno != ERANGE);
+		
+		errno = 0;
+		ASSERT("T310 STRTOU64 MAX BASE 16",
+				strtou64(i16, NULL, 16) == UINT64_MAX && errno != ERANGE);
+	}
+	
+	{
+		char i[] = "-153";
+		
+		errno = 0;
+		ASSERT("T311 STRTOU64 NEGATIVE",
+				!strtou64(i, NULL, 10) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T312 STRTOU64 BASE 1",
+				!strtou64(i, NULL, 1) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T313 STRTOU64 BASE -1",
+				!strtou64(i, NULL, -1) && errno == EINVAL);
+		
+		errno = 0;
+		ASSERT("T314 STRTOU64 BASE 37",
+				!strtou64(i, NULL, 37) && errno == EINVAL);
+	}
+	
+	{
+		char i[] = "18446744073709551616";
+		
+		errno = 0;
+		ASSERT("T315 STRTOU64 OVERFLOW",
+				strtou64(i, NULL, 10) == UINT64_MAX && errno == ERANGE);
+	}
+	
+	printf("%d of %d tests passed\n", testspassed, testsrun);
+	return testsfailed;
+}