Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ambrop72/badvpn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorambrop7 <ambrop7@1a93d707-3861-5ebc-ad3b-9740d49b5140>2012-10-20 19:17:19 +0400
committerambrop7 <ambrop7@1a93d707-3861-5ebc-ad3b-9740d49b5140>2012-10-20 19:17:19 +0400
commitd8cde9e2c47df2cd704c39faf5034003b152d2e2 (patch)
tree5466d6bf7c2537221e0e960f31034f1475179de1 /examples
parentaef19f5e217af0fe4ddd8c1a1c2f6237beb424d7 (diff)
examples: add parse_number_test.c
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt1
-rw-r--r--examples/parse_number_test.c110
2 files changed, 111 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 3a3b636..0157fe1 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -75,6 +75,7 @@ add_executable(substring_test substring_test.c)
if (NOT WIN32)
add_executable(ipaddr6_test ipaddr6_test.c)
+ add_executable(parse_number_test parse_number_test.c)
endif ()
if (BUILDING_RANDOM)
diff --git a/examples/parse_number_test.c b/examples/parse_number_test.c
new file mode 100644
index 0000000..08f6e33
--- /dev/null
+++ b/examples/parse_number_test.c
@@ -0,0 +1,110 @@
+/**
+ * @file parse_number_test.c
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ *
+ * @section LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the author nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <time.h>
+
+#include <misc/parse_number.h>
+#include <misc/debug.h>
+
+static void test_random (int num_digits, int digit_modulo)
+{
+ ASSERT(num_digits > 0);
+ ASSERT(digit_modulo > 0);
+
+ uint8_t digits[40];
+
+ for (int i = 0; i < num_digits; i++) {
+ digits[i] = '0' + (rand() % digit_modulo);
+ }
+ digits[num_digits] = '\0';
+
+ char *endptr;
+ uintmax_t std_num = strtoumax((const char *)digits, &endptr, 10);
+ int std_res = !*endptr && !(std_num == UINTMAX_MAX && errno == ERANGE);
+
+ uintmax_t num = 0;
+ int res = parse_unsigned_integer_bin((const char *)digits, num_digits, &num);
+
+ if (res != std_res) {
+ printf("fail1 %s\n", (const char *)digits);
+ ASSERT_FORCE(0);
+ }
+
+ if (res && num != std_num) {
+ printf("fail2 %s\n", (const char *)digits);
+ ASSERT_FORCE(0);
+ }
+}
+
+static void test_value (uintmax_t x)
+{
+ char str[40];
+ sprintf(str, "%" PRIuMAX, x);
+ uintmax_t y;
+ int res = parse_unsigned_integer_bin(str, strlen(str), &y);
+ ASSERT_FORCE(res);
+ ASSERT_FORCE(y == x);
+}
+
+static void test_value_range (uintmax_t start, uintmax_t count)
+{
+ uintmax_t i = start;
+ do {
+ test_value(i);
+ i++;
+ } while (i != start + count);
+}
+
+int main ()
+{
+ srand(time(NULL));
+
+ for (int num_digits = 1; num_digits <= 22; num_digits++) {
+ for (int i = 0; i < 1000000; i++) {
+ test_random(num_digits, 10);
+ }
+ for (int i = 0; i < 1000000; i++) {
+ test_random(num_digits, 11);
+ }
+ }
+
+ test_value_range(UINTMAX_C(0), 5000000);
+ test_value_range(UINTMAX_C(100000000), 5000000);
+ test_value_range(UINTMAX_C(258239003), 5000000);
+ test_value_range(UINTMAX_C(8241096180752634), 5000000);
+ test_value_range(UINTMAX_C(9127982390882308083), 5000000);
+ test_value_range(UINTMAX_C(18446744073700000000), 20000000);
+
+ return 0;
+}