todo: Suggestion for UUIDs in /etc/fstab (thanks Joshua Daniel Franklin).
[libguestfs.git] / generator / generator_capitests.ml
index 2cad2ae..f9cacf2 100644 (file)
@@ -113,6 +113,22 @@ md5sum (const char *filename, char *result)
   result[32] = '\\0';
 }
 
+/* Return the value for a key in a hashtable.
+ * Note: the return value is part of the hash and should not be freed.
+ */
+static const char *
+get_key (char **hash, const char *key)
+{
+  size_t i;
+
+  for (i = 0; hash[i] != NULL; i += 2) {
+    if (STREQ (hash[i], key))
+      return hash[i+1];
+  }
+
+  return NULL; /* key not found */
+}
+
 ";
 
   (* Generate a list of commands which are not tested anywhere. *)
@@ -695,6 +711,28 @@ and generate_one_test_body name i test_name init test =
       in
       List.iter (generate_test_command_call test_name) seq;
       generate_test_command_call ~test test_name last
+  | TestOutputHashtable (seq, fields) ->
+      pr "  /* TestOutputHashtable for %s (%d) */\n" name i;
+      pr "  const char *key, *expected, *value;\n";
+      let seq, last = get_seq_last seq in
+      let test () =
+        List.iter (
+          fun (key, value) ->
+            pr "    key = \"%s\";\n" (c_quote key);
+            pr "    expected = \"%s\";\n" (c_quote value);
+            pr "    value = get_key (r, key);\n";
+            pr "    if (value == NULL) {\n";
+            pr "      fprintf (stderr, \"%s: key \\\"%%s\\\" not found in hash: expecting \\\"%%s\\\"\\n\", key, expected);\n" test_name;
+            pr "      return -1;\n";
+            pr "    }\n";
+            pr "    if (STRNEQ (value, expected)) {\n";
+            pr "      fprintf (stderr, \"%s: key \\\"%%s\\\": expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", key, expected, value);\n" test_name;
+            pr "      return -1;\n";
+            pr "    }\n";
+        ) fields
+      in
+      List.iter (generate_test_command_call test_name) seq;
+      generate_test_command_call ~test test_name last
   | TestLastFail seq ->
       pr "  /* TestLastFail for %s (%d) */\n" name i;
       let seq, last = get_seq_last seq in
@@ -776,8 +814,8 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
 
       if optargs <> [] then (
         pr "    struct guestfs_%s_argv optargs;\n" name;
-        let bitmask = List.fold_left (
-          fun bitmask optarg ->
+        let _, bitmask = List.fold_left (
+          fun (shift, bitmask) optarg ->
             let is_set =
               match optarg with
               | Bool n, "" -> false
@@ -803,10 +841,11 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
               | String n, arg ->
                   pr "    optargs.%s = \"%s\";\n" n (c_quote arg); true
               | _ -> assert false in
-            let bitmask = Int64.shift_left bitmask 1 in
-            let bitmask = if is_set then Int64.succ bitmask else bitmask in
-            bitmask
-        ) 0L optargs in
+            let bit = if is_set then Int64.shift_left 1L shift else 0L in
+            let bitmask = Int64.logor bitmask bit in
+            let shift = shift + 1 in
+            (shift, bitmask)
+        ) (0, 0L) optargs in
         pr "    optargs.bitmask = UINT64_C(0x%Lx);\n" bitmask;
       );