chiark / gitweb /
Infrastructure: Split the files into subdirectories.
[mLib] / struct / sym-gtest
diff --git a/struct/sym-gtest b/struct/sym-gtest
new file mode 100755 (executable)
index 0000000..ac5bcaf
--- /dev/null
@@ -0,0 +1,51 @@
+#! /usr/bin/perl
+#
+# Generate a random test file for symbol table testing.
+#
+# Syntax reference:
+#
+#  set k v -- assign (textual) key k the (integer) value i
+#  get k -- display value at key k
+#  del k -- remove key k
+#  count -- show number of items
+#  show -- dump out the entire table, in alphabetical key order
+
+sub random ($) {
+  my $lim = shift;
+  return int(rand($lim));
+}
+
+$words = "/usr/dict/words";
+-r $words or $words = "/usr/share/dict/words";
+open(DICT, $words) or die("open($words): $!");
+@w = grep /./, <DICT>;
+chomp(@w);
+close(DICT);
+
+%a = ();
+
+$lines = shift || 100;
+$serial = 1;
+while ($lines) {
+  $what = random(4);
+  if ($what == 0) {
+    my $k = $w[random(@w)];
+    my $i = $serial++;
+    print "set $k $i\n";
+    $a{$k} = $i;
+  } elsif ($what == 1) {
+    my $k = $w[random(@w)];
+    if (!exists($a{$w}) && random(8) != 0) { next; }
+    print "get $k\n";
+  } elsif ($what == 2) {
+    my $k = $w[random(@w)];
+    if (!exists($a{$w}) && random(8) != 0) { next; }
+    print "del $k\n";
+    delete($w{$k});
+  } elsif (random(5) == 0) {
+    print "count\n";
+  } else { next; }
+  $lines--;
+}
+
+print "show\n";