timer: handle time going backwards.
[ccan] / ccan / opt / test / run-helpers.c
index 8cf229cc6acc230fd6e32bd794938f24c967d141..0a08a85f7aa3930011cba5600186f3a21d5c5f09 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <limits.h>
 #include "utils.h"
 #include <stdlib.h>
 #include <limits.h>
 #include "utils.h"
+#include <math.h>
 
 /* We don't actually want it to exit... */
 static jmp_buf exited;
 
 /* We don't actually want it to exit... */
 static jmp_buf exited;
@@ -19,9 +20,6 @@ static int saved_fprintf(FILE *ignored, const char *fmt, ...);
 #define vfprintf(f, fmt, ap) saved_vprintf(fmt, ap)
 static int saved_vprintf(const char *fmt, va_list ap);
 
 #define vfprintf(f, fmt, ap) saved_vprintf(fmt, ap)
 static int saved_vprintf(const char *fmt, va_list ap);
 
-#define malloc(size) saved_malloc(size)
-static void *saved_malloc(size_t size);
-
 #include <ccan/opt/helpers.c>
 #include <ccan/opt/opt.c>
 #include <ccan/opt/usage.c>
 #include <ccan/opt/helpers.c>
 #include <ccan/opt/opt.c>
 #include <ccan/opt/usage.c>
@@ -54,7 +52,7 @@ static int saved_printf(const char *fmt, ...)
        return ret;
 }
 
        return ret;
 }
 
-static int saved_fprintf(FILE *ignored, const char *fmt, ...)
+static int saved_fprintf(FILE *ignored UNNEEDED, const char *fmt, ...)
 {
        va_list ap;
        int ret;
 {
        va_list ap;
        int ret;
@@ -65,17 +63,22 @@ static int saved_fprintf(FILE *ignored, const char *fmt, ...)
        return ret;
 }
 
        return ret;
 }
 
-#undef malloc
-static void *last_allocation;
-static void *saved_malloc(size_t size)
+static void set_args(int *argc, char ***argv, ...)
 {
 {
-       return last_allocation = malloc(size);
+       va_list ap;
+       *argv = malloc(sizeof(**argv) * 20);
+
+       va_start(ap, argv);
+       for (*argc = 0;
+            ((*argv)[*argc] = va_arg(ap, char*)) != NULL;
+            (*argc)++);
+       va_end(ap);
 }
 
 /* Test helpers. */
 int main(int argc, char *argv[])
 {
 }
 
 /* Test helpers. */
 int main(int argc, char *argv[])
 {
-       plan_tests(452);
+       plan_tests(500);
 
        /* opt_set_bool */
        {
 
        /* opt_set_bool */
        {
@@ -120,7 +123,7 @@ int main(int argc, char *argv[])
        }
        /* opt_set_charp */
        {
        }
        /* opt_set_charp */
        {
-               char *arg = (char *)"wrong";
+               char *arg = cast_const(char *, "wrong");
                reset_options();
                opt_register_arg("-a", opt_set_charp, NULL, &arg, "All");
                ok1(parse_args(&argc, &argv, "-a", "string", NULL));
                reset_options();
                opt_register_arg("-a", opt_set_charp, NULL, &arg, "All");
                ok1(parse_args(&argc, &argv, "-a", "string", NULL));
@@ -204,6 +207,58 @@ int main(int argc, char *argv[])
                else
                        fail("FIXME: Handle other long sizes");
        }
                else
                        fail("FIXME: Handle other long sizes");
        }
+       /* opt_set_floatval */
+       {
+               float arg = 1000;
+               reset_options();
+               opt_register_arg("-a", opt_set_floatval, NULL, &arg, "All");
+               ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
+               ok1(arg == 9999);
+               ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
+               ok1(arg == -9999);
+               ok1(parse_args(&argc, &argv, "-a", "1e33", NULL));
+               ok1(arg == 1e33f);
+               /*overflows should fail */
+               ok1(!parse_args(&argc, &argv, "-a", "1e39", NULL));
+               ok1(!parse_args(&argc, &argv, "-a", "-1e40", NULL));
+               /*low numbers lose precision but work */
+               ok1(parse_args(&argc, &argv, "-a", "1e-39", NULL));
+               ok1(arg == 1e-39f);
+               ok1(parse_args(&argc, &argv, "-a", "-1e-45", NULL));
+               ok1(arg == -1e-45f);
+               ok1(!parse_args(&argc, &argv, "-a", "1e-99", NULL));
+               ok1(parse_args(&argc, &argv, "-a", "0", NULL));
+               ok1(arg == 0);
+               ok1(parse_args(&argc, &argv, "-a", "1.111111111111", NULL));
+               ok1(arg == 1.1111112f);
+               ok1(parse_args(&argc, &argv, "-a", "INF", NULL));
+               ok1(isinf(arg));
+               ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
+               ok1(!parse_args(&argc, &argv, "-a", "1e7crap", NULL));
+       }
+       /* opt_set_doubleval */
+       {
+               double arg = 1000;
+               reset_options();
+               opt_register_arg("-a", opt_set_doubleval, NULL, &arg, "All");
+               ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
+               ok1(arg == 9999);
+               ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
+               ok1(arg == -9999);
+               ok1(parse_args(&argc, &argv, "-a", "1e-299", NULL));
+               ok1(arg == 1e-299);
+               ok1(parse_args(&argc, &argv, "-a", "-1e-305", NULL));
+               ok1(arg == -1e-305);
+               ok1(!parse_args(&argc, &argv, "-a", "1e-499", NULL));
+               ok1(parse_args(&argc, &argv, "-a", "0", NULL));
+               ok1(arg == 0);
+               ok1(parse_args(&argc, &argv, "-a", "1.1111111111111111111", NULL));
+               ok1(arg == 1.1111111111111112);
+               ok1(parse_args(&argc, &argv, "-a", "INF", NULL));
+               ok1(isinf(arg));
+               ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
+               ok1(!parse_args(&argc, &argv, "-a", "1e7crap", NULL));
+       }
 
        {
                const long long k = 1024;
 
        {
                const long long k = 1024;
@@ -985,11 +1040,7 @@ int main(int argc, char *argv[])
                /* parse_args allocates argv */
                free(argv);
 
                /* parse_args allocates argv */
                free(argv);
 
-               argc = 2;
-               argv = malloc(sizeof(argv[0]) * 3);
-               argv[0] = (char *)"thisprog";
-               argv[1] = (char *)"-a";
-               argv[2] = NULL;
+               set_args(&argc, &argv, "thisprog", "-a", NULL);
 
                exitval = setjmp(exited);
                if (exitval == 0) {
 
                exitval = setjmp(exited);
                if (exitval == 0) {
@@ -997,6 +1048,8 @@ int main(int argc, char *argv[])
                        fail("opt_show_version_and_exit returned?");
                } else {
                        ok1(exitval - 1 == 0);
                        fail("opt_show_version_and_exit returned?");
                } else {
                        ok1(exitval - 1 == 0);
+                       /* We should have freed table!. */
+                       ok1(opt_table == NULL);
                }
                ok1(strcmp(output, "1.2.3\n") == 0);
                free(output);
                }
                ok1(strcmp(output, "1.2.3\n") == 0);
                free(output);
@@ -1011,11 +1064,7 @@ int main(int argc, char *argv[])
                opt_register_noarg("-a",
                                   opt_usage_and_exit, "[args]", "");
 
                opt_register_noarg("-a",
                                   opt_usage_and_exit, "[args]", "");
 
-               argc = 2;
-               argv = malloc(sizeof(argv[0]) * 3);
-               argv[0] = (char *)"thisprog";
-               argv[1] = (char *)"-a";
-               argv[2] = NULL;
+               set_args(&argc, &argv, "thisprog", "-a", NULL);
 
                exitval = setjmp(exited);
                if (exitval == 0) {
 
                exitval = setjmp(exited);
                if (exitval == 0) {
@@ -1023,14 +1072,14 @@ int main(int argc, char *argv[])
                        fail("opt_usage_and_exit returned?");
                } else {
                        ok1(exitval - 1 == 0);
                        fail("opt_usage_and_exit returned?");
                } else {
                        ok1(exitval - 1 == 0);
+                       /* We should have freed table!. */
+                       ok1(opt_table == NULL);
                }
                ok1(strstr(output, "[args]"));
                ok1(strstr(output, argv[0]));
                }
                ok1(strstr(output, "[args]"));
                ok1(strstr(output, argv[0]));
-               ok1(strstr(output, "[-a]"));
+               ok1(strstr(output, "\n-a"));
                free(output);
                free(argv);
                free(output);
                free(argv);
-               /* It exits without freeing usage string. */
-               free(last_allocation);
                output = NULL;
        }
 
                output = NULL;
        }
 
@@ -1144,17 +1193,47 @@ int main(int argc, char *argv[])
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
 
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
 
+       /* opt_show_floatval */
+       {
+               float f;
+               char buf[OPT_SHOW_LEN+2] = { 0 };
+               buf[OPT_SHOW_LEN] = '!';
+
+               f = -77.5;
+               opt_show_floatval(buf, &f);
+               ok1(strcmp(buf, "-77.500000") == 0);
+               ok1(buf[OPT_SHOW_LEN] == '!');
+
+               f = 77.5;
+               opt_show_floatval(buf, &f);
+               ok1(strcmp(buf, "77.500000") == 0);
+               ok1(buf[OPT_SHOW_LEN] == '!');
+       }
+
+       /* opt_show_doubleval */
+       {
+               double d;
+               char buf[OPT_SHOW_LEN+2] = { 0 };
+               buf[OPT_SHOW_LEN] = '!';
+
+               d = -77;
+               opt_show_doubleval(buf, &d);
+               ok1(strcmp(buf, "-77.000000") == 0);
+               ok1(buf[OPT_SHOW_LEN] == '!');
+
+               d = 77;
+               opt_show_doubleval(buf, &d);
+               ok1(strcmp(buf, "77.000000") == 0);
+               ok1(buf[OPT_SHOW_LEN] == '!');
+       }
+
        /* opt_log_stderr. */
        {
                reset_options();
                opt_register_noarg("-a",
                                   opt_usage_and_exit, "[args]", "");
 
        /* opt_log_stderr. */
        {
                reset_options();
                opt_register_noarg("-a",
                                   opt_usage_and_exit, "[args]", "");
 
-               argc = 2;
-               argv = malloc(sizeof(argv[0]) * 3);
-               argv[0] = (char *)"thisprog";
-               argv[1] = (char *)"--garbage";
-               argv[2] = NULL;
+               set_args(&argc, &argv, "thisprog", "--garbage", NULL);
                ok1(!opt_parse(&argc, argv, opt_log_stderr));
                ok1(!strcmp(output,
                            "thisprog: --garbage: unrecognized option\n"));
                ok1(!opt_parse(&argc, argv, opt_log_stderr));
                ok1(!strcmp(output,
                            "thisprog: --garbage: unrecognized option\n"));
@@ -1169,11 +1248,7 @@ int main(int argc, char *argv[])
                reset_options();
                opt_register_noarg("-a",
                                   opt_usage_and_exit, "[args]", "");
                reset_options();
                opt_register_noarg("-a",
                                   opt_usage_and_exit, "[args]", "");
-               argc = 2;
-               argv = malloc(sizeof(argv[0]) * 3);
-               argv[0] = (char *)"thisprog";
-               argv[1] = (char *)"--garbage";
-               argv[2] = NULL;
+               set_args(&argc, &argv, "thisprog", "--garbage", NULL);
                exitval = setjmp(exited);
                if (exitval == 0) {
                        opt_parse(&argc, argv, opt_log_stderr_exit);
                exitval = setjmp(exited);
                if (exitval == 0) {
                        opt_parse(&argc, argv, opt_log_stderr_exit);