summaryrefslogtreecommitdiff
path: root/test/pkstn.cpp
blob: 8a4d901b7019d0a2eab892e02b03bd8e3587f429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

#include "../pkstn.h"

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <type_traits>

const char *STRNUM_ONE = "1";
const char *STRNUM_ONE_MINUS = "-1";
const char *STRNUM_UNDERFLOW = "-99999999999999999999999999999999";
const char *STRNUM_OVERFLOW = "99999999999999999999999999999999";
const char *STRNUM_UNDERFLOW_FLOAT = "-1.0e9999";
const char *STRNUM_OVERFLOW_FLOAT = "1.0e9999";
const char *STRNUM_INCONVERTIBLE = "junk";

enum STRNUM_TESTS : uint8_t {
	TEST_ONE               = 0x01,
	TEST_ONE_MINUS         = 0x02,
	TEST_UNDERFLOW         = 0x04,
	TEST_OVERFLOW          = 0x08,
	TEST_INCONVERTIBLE     = 0x10,
	TEST_NO_NEG            = TEST_ONE | TEST_OVERFLOW | TEST_INCONVERTIBLE,
	TEST_FLOATS            = TEST_ONE | TEST_ONE_MINUS | TEST_UNDERFLOW | TEST_OVERFLOW,
	TEST_ALL               = 0xFF,
};

template <typename T, enum STRNUM_TESTS t>
void test() {
	enum PK_STN_RES res = {};
	T i = {0};
	if constexpr (t & TEST_ONE) {
		res = pk_stn(&i, STRNUM_ONE);
		std::cout << "pkstn: TEST_ONE res: " << i << "\n";
		if (res != PK_STN_RES_SUCCESS) exit(1);
		if (1 != i) exit(1);
	}
	if constexpr (t & TEST_ONE_MINUS) {
		res = pk_stn(&i, STRNUM_ONE_MINUS);
		std::cout << "pkstn: STRNUM_ONE_MINUS res: " << i << "\n";
		if (res != PK_STN_RES_SUCCESS) exit(1);
		if (T(-1) != i) exit(1);
	}
	if constexpr (t & TEST_UNDERFLOW) {
		const char * str;
		if constexpr(std::is_same<T, float>::value == true || std::is_same<T, double>::value == true) {
			str = STRNUM_UNDERFLOW_FLOAT;
		}	else {
			str = STRNUM_UNDERFLOW;
		}
		res = pk_stn(&i, str);
		std::cout << "pkstn: TEST_UNDERFLOW res: " << i << "\n";
		if constexpr(std::is_unsigned_v<T> == true) {
			if (res != PK_STN_RES_OVERFLOW) exit(1);
		} else {
			if (res != PK_STN_RES_UNDERFLOW) exit(1);
		}
	}
	if constexpr (t & TEST_OVERFLOW) {
		const char * str;
		if constexpr(std::is_same<T, float>::value == true || std::is_same<T, double>::value == true) {
			str = STRNUM_OVERFLOW_FLOAT;
		}	else {
			str = STRNUM_OVERFLOW;
		}
		res = pk_stn(&i, str);
		std::cout << "pkstn: TEST_OVERFLOW res: " << i << "\n";
		if (res != PK_STN_RES_OVERFLOW) exit(1);
	}
	if constexpr (t & TEST_INCONVERTIBLE) {
		res = pk_stn(&i, STRNUM_INCONVERTIBLE);
		std::cout << "pkstn: TEST_INCONVERTIBLE res: " << i << "\n";
		if (res != PK_STN_RES_INCONVERTIBLE) exit(1);
	}
};

int main(int argc, char *argv[])
{
	(void)argc;
	(void)argv;
	fprintf(stdout, "\n");

	// stn_int64_t
	{
		fprintf(stdout, "\npkstn: starting int64_t\n");
		test<int64_t, TEST_ALL>();
	}

	// stn_uint64_t
	{
		fprintf(stdout, "\npkstn: starting uint64_t\n");
		test<uint64_t, TEST_ALL>(); }

	// stn_int32_t
	{
		fprintf(stdout, "\npkstn: starting int32_t\n");
		test<int32_t, TEST_ALL>();
	}

	// stn_uint32_t
	{
		fprintf(stdout, "\npkstn: starting uint32_t\n");
		test<uint32_t, TEST_ALL>();
	}

	// stn_int16_t
	{
		fprintf(stdout, "\npkstn: starting int16_t\n");
		test<int16_t, TEST_ALL>();
	}

	// stn_uint16_t
	{
		fprintf(stdout, "\npkstn: starting uint16_t\n");
		test<uint16_t, TEST_ALL>();
	}

	// stn_int8_t
	{
		fprintf(stdout, "\npkstn: starting int8_t\n");
		test<int8_t, TEST_ALL>();
	}

	// stn_uint8_t
	{
		fprintf(stdout, "\npkstn: starting uint8_t\n");
		test<uint8_t, TEST_ALL>();
	}

	// stn_float
	{
		fprintf(stdout, "\npkstn: starting float\n");
		test<float, TEST_FLOATS>();
	}

	// stn_double
	{
		fprintf(stdout, "\npkstn: starting double\n");
		test<double, TEST_FLOATS>();
	}

	return 0;
}