Changeset: 25eed45e80b7 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/25eed45e80b7
Added Files:
        clients/examples/C/murltest.c
        clients/examples/C/murltest.h
        clients/examples/C/testsfile.c
        clients/mapilib/Tests/All
        clients/mapilib/Tests/murltest.py
        clients/mapilib/Tests/tests.md
        clients/mapilib/msettings.c
        clients/mapilib/msettings.h
        clients/mapilib/parseurl.c
Modified Files:
        clients/examples/C/CMakeLists.txt
        clients/mapilib/CMakeLists.txt
        clients/mapilib/mapi.c
Branch: monetdburl
Log Message:

Import msettings plus tests


diffs (truncated from 3337 to 300 lines):

diff --git a/clients/examples/C/CMakeLists.txt 
b/clients/examples/C/CMakeLists.txt
--- a/clients/examples/C/CMakeLists.txt
+++ b/clients/examples/C/CMakeLists.txt
@@ -80,6 +80,17 @@ target_link_libraries(bincopyloops
   copybinary
   )
 
+add_executable(murltest
+  murltest.c
+  testsfile.c
+  murltest.h
+)
+
+target_link_libraries(murltest
+  PRIVATE
+  mapi
+)
+
 install(TARGETS
   sample0
   sample1
@@ -89,6 +100,7 @@ install(TARGETS
   streamcat
   testcondvar
   bincopydata
+  murltest
   RUNTIME
   DESTINATION
   ${CMAKE_INSTALL_BINDIR}
@@ -105,6 +117,7 @@ if(WIN32)
     $<TARGET_PDB_FILE:testcondvar>
     $<TARGET_PDB_FILE:bincopydata>
     $<TARGET_PDB_FILE:bincopyloops>
+    $<TARGET_PDB_FILE:murltest>
     DESTINATION ${CMAKE_INSTALL_BINDIR}
     OPTIONAL)
 endif()
diff --git a/clients/examples/C/murltest.c b/clients/examples/C/murltest.c
new file mode 100644
--- /dev/null
+++ b/clients/examples/C/murltest.c
@@ -0,0 +1,83 @@
+#define _POSIX_C_SOURCE 200809L
+
+#include "murltest.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *USAGE = "Usage: murltest TESTFILES..";
+
+static bool
+run_file(const char *filename, int verbose)
+{
+       FILE *to_close, *f;
+       if (strcmp(filename, "-") == 0) {
+               f = stdin;
+               to_close = NULL;
+       } else {
+               f = fopen(filename, "r");
+               if (!f) {
+                       fprintf(stderr, "Could not open %s: %s\n", filename, 
strerror(errno));
+                       return false;
+               }
+               to_close = f;
+       }
+
+       bool ok = run_tests(filename, f, verbose);
+
+       if (to_close)
+               fclose(to_close);
+       return ok;
+}
+
+static bool run_files(char **files, int verbose)
+{
+       while (*files) {
+               if (!run_file(*files, verbose))
+                       return false;
+               files++;
+       }
+       return true;
+}
+
+int
+main(int argc, char **argv)
+{
+       int verbose = 0;
+
+       char **files = calloc(argc + 1, sizeof(char*));
+       if (!files)
+               return 3;
+       char **next_slot = &files[0];
+       for (int i = 1; i < argc; i++) {
+               char *arg = argv[i];
+               if (arg[0] != '-') {
+                       *next_slot++ = arg;
+                       continue;
+               }
+               if (arg[1] == 'v') {
+                       char *p = &arg[1];
+                       while (*p == 'v') {
+                               p++;
+                               verbose++;
+                       }
+                       if (*p == '\0')
+                               continue;
+                       fprintf(stderr, "invalid letter %c in flag %s\n", *p, 
arg);
+                       free(files);
+                       return 1;
+               } else {
+                       fprintf(stderr, "invalid flag %s\n", arg);
+                       free(files);
+                       return 1;
+               }
+       }
+
+       bool ok = run_files(files, verbose);
+
+       free(files);
+       return ok ? EXIT_SUCCESS : EXIT_FAILURE;
+}
diff --git a/clients/examples/C/murltest.h b/clients/examples/C/murltest.h
new file mode 100644
--- /dev/null
+++ b/clients/examples/C/murltest.h
@@ -0,0 +1,4 @@
+#include <stdbool.h>
+#include <stdio.h>
+
+bool run_tests(const char *filename, FILE *f, int verbose);
diff --git a/clients/examples/C/testsfile.c b/clients/examples/C/testsfile.c
new file mode 100644
--- /dev/null
+++ b/clients/examples/C/testsfile.c
@@ -0,0 +1,402 @@
+#define _POSIX_C_SOURCE 200809L
+
+#include "murltest.h"
+#include "msettings.h"
+
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+// love it when a task is so straightforward I can use global variables!
+static int start_line = -1;
+static int nstarted = 0;
+static msettings *mp = NULL;
+
+static bool
+handle_parse_command(const char *location, char *url)
+{
+       char *errmsg = NULL;
+       bool ok = msettings_parse_url(mp, url, &errmsg);
+       if (!ok) {
+               assert(errmsg);
+               fprintf(stderr, "%s: %s\n", location, errmsg);
+               free(errmsg);
+               return false;
+       }
+       return true;
+}
+
+static bool
+handle_accept_command(const char *location, char *url)
+{
+       char *errmsg = NULL;
+       bool ok = msettings_parse_url(mp, url, &errmsg);
+       if (!ok) {
+               assert(errmsg);
+               fprintf(stderr, "%s: %s\n", location, errmsg);
+               free(errmsg);
+               return false;
+       }
+
+       const char *msg = msettings_validate(mp);
+       if (msg != NULL) {
+               fprintf(stderr, "%s: URL invalid: %s\n", location, msg);
+               return false;
+       }
+       return true;
+}
+
+static bool
+handle_reject_command(const char *location, char *url)
+{
+       bool ok = msettings_parse_url(mp, url, NULL);
+       if (!ok)
+               return true;
+
+       const char *msg = msettings_validate(mp);
+       if (msg != NULL)
+               return true;
+
+       fprintf(stderr, "%s: expected URL to be rejected.\n", location);
+       return false;
+}
+
+static bool
+handle_set_command(const char *location, const char *key, const char *value)
+{
+       msettings_error msg = msetting_set_named(mp, true, key, value);
+       if (msg) {
+               fprintf(stderr, "%s: cannot set '%s': %s\n", location, key, 
msg);
+               return false;
+       }
+       return true;
+}
+
+static bool
+ensure_valid(const char *location) {
+       const char *msg = msettings_validate(mp);
+       if (msg == NULL)
+               return true;
+       fprintf(stderr, "%s: invalid parameter state: %s\n", location, msg);
+       return false;
+}
+
+static bool
+expect_bool(const char *location, const mparm parm, bool (*extract)(const 
msettings*), const char *value)
+{
+       int x = msetting_parse_bool(value);
+       if (x < 0) {
+               fprintf(stderr, "%s: syntax error: invalid bool '%s'\n", 
location, value);
+       }
+       bool b = x > 0;
+
+       bool actual;
+       if (extract) {
+               if (!ensure_valid(location))
+                       return false;
+               actual = extract(mp);
+       } else {
+               actual = msetting_bool(mp, parm);
+       }
+
+       if (actual == b)
+               return true;
+
+       char *b_ = b ? "true" : "false";
+       char *actual_ = actual ? "true" : "false";
+       fprintf(stderr, "%s: expected %s, found %s\n", location, b_, actual_);
+       return false;
+
+}
+
+static bool
+expect_long(const char *location, const mparm parm, long (*extract)(const 
msettings*), const char *value)
+{
+       if (strlen(value) == 0) {
+               fprintf(stderr, "%s: syntax error: integer value cannot be 
empty string\n", location);
+               return false;
+       }
+       char *end = NULL;
+       long n = strtol(value, &end, 10);
+       if (*end != '\0') {
+               fprintf(stderr, "%s: syntax error: invalid integer '%s'\n", 
location, value);
+               return false;
+       }
+
+       long actual;
+       if (extract) {
+               if (!ensure_valid(location))
+                       return false;
+               actual = extract(mp);
+       } else {
+               actual = msetting_long(mp, parm);
+       }
+
+       if (actual == n)
+               return true;
+
+       fprintf(stderr, "%s: expected %ld, found %ld\n", location, n, actual);
+       return false;
+}
+
+static bool
+expect_string(const char *location, const mparm parm, const char 
*(*extract)(const msettings*), const char *value)
+{
+       const char *actual;
+       if (extract) {
+               if (!ensure_valid(location))
+                       return false;
+               actual = extract(mp);
+       } else {
+               actual = msetting_string(mp, parm);
+       }
+
+       if (strcmp(actual, value) == 0)
+               return true;
+
+       fprintf(stderr, "%s: expected '%s', found '%s'\n", location, value, 
actual);
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to