about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJesse Ruderman <jruderman@gmail.com>2011-09-26 20:34:07 -0700
committerJesse Ruderman <jruderman@gmail.com>2011-09-26 20:34:07 -0700
commit4ff46a1502461970e50c17a3da0b8bb76c732766 (patch)
tree6356e761c1ac020a00441cb3f5de155c90d1693b
parent3778b6c6a80a5ebced4782f1e4462ff82471847b (diff)
downloadrust-4ff46a1502461970e50c17a3da0b8bb76c732766.tar.gz
rust-4ff46a1502461970e50c17a3da0b8bb76c732766.zip
Add a cycle-collection fuzzer
-rw-r--r--src/fuzzer/cycles.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/fuzzer/cycles.rs b/src/fuzzer/cycles.rs
new file mode 100644
index 00000000000..0caf414d136
--- /dev/null
+++ b/src/fuzzer/cycles.rs
@@ -0,0 +1,59 @@
+use std;
+import std::vec;
+import std::rand;
+import std::option;
+
+// random uint less than n
+fn under(r : rand::rng, n : uint) -> uint { assert n != 0u; r.next() as uint % n }
+
+// random choice from a vec
+fn choice<T>(r : rand::rng, v : [T]) -> T { assert vec::len(v) != 0u; v[under(r, vec::len(v))] }
+
+// 1 in n chance of being true
+fn unlikely(r : rand::rng, n : uint) -> bool { under(r, n) == 0u }
+
+tag maybe_pointy {
+  no_pointy;
+  yes_pointy(@pointy);
+}
+
+type pointy = {
+  mutable x : maybe_pointy,
+  mutable y : maybe_pointy,
+  mutable z : fn()->()
+};
+
+iter allunder(n: uint) -> uint {
+    let i: uint = 0u;
+    while i < n { put i; i += 1u; }
+}
+
+fn nopT(_x : @pointy) { }
+fn nop() { }
+
+fn test_cycles(r : rand::rng)
+{
+    const max : uint = 10u;
+
+    let v : [mutable @pointy] = [mutable];
+    for each i in allunder(max) {
+        v += [mutable @{ mutable x : no_pointy, mutable y : no_pointy, mutable z: nop }];
+    }
+
+    for each i in allunder(max) {
+        v[i].x = yes_pointy(v[under(r, max)]);
+        v[i].y = yes_pointy(v[under(r, max)]);
+        v[i].z = bind nopT(v[under(r, max)]);
+    }
+
+    // Drop refs one at a time
+    for each i in allunder(max) {
+        v[i] = @{ mutable x : no_pointy, mutable y : no_pointy, mutable z: nop };
+    }
+}
+
+fn main()
+{
+    let r = rand::mk_rng();
+    test_cycles(r);
+}