about summary refs log tree commit
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_gc.rb39
-rw-r--r--test/test_match_data.rb46
2 files changed, 85 insertions, 0 deletions
diff --git a/test/test_gc.rb b/test/test_gc.rb
new file mode 100644
index 0000000..d567696
--- /dev/null
+++ b/test/test_gc.rb
@@ -0,0 +1,39 @@
+require 'test/unit'
+require 'rpatricia'
+
+# this takes a while, watch memory usage not grow in top(1) or similar
+
+class TestGc < Test::Unit::TestCase
+
+  def setup
+    @arrays = Patricia.new
+    @arrays.add('10.0.0.0/8', [])
+    @arrays.add('127.0.0.0/24', [])
+
+    @strings = Patricia.new
+    @strings.add('10.0.0.0/8', "big lan")
+    @strings.add('127.0.0.0/24', "localhost")
+  end
+
+  def test_gc
+    assert_nothing_raised do
+      10_000_000.times do
+        t = Patricia.new
+        t.add('10.0.0.0/8', {})
+        t.add('127.0.0.0/24', "home sweet home")
+      end
+    end
+
+    # ensure what we created originally didn't get GC-ed'
+    100.times do
+      assert_equal [], @arrays.match_best('127.0.0.1').data
+      assert_equal "localhost", @strings.match_best('127.0.0.1').data
+    end
+  end
+
+  def test_destroy
+    assert @strings.destroy
+    assert @strings.destroy
+  end
+
+end
diff --git a/test/test_match_data.rb b/test/test_match_data.rb
new file mode 100644
index 0000000..e85a015
--- /dev/null
+++ b/test/test_match_data.rb
@@ -0,0 +1,46 @@
+require 'test/unit'
+require 'rpatricia'
+
+class TestMatchData < Test::Unit::TestCase
+
+  def test_match_data_non_string
+    ary = []
+    t = Patricia.new
+    t.add('10.0.0.0/8', ary)
+
+    z = t.match_best '10.1.1.1'
+    assert_equal ary.object_id, z.data.object_id
+    z.data << '10.1.1.1'
+
+    t.match_best('10.2.2.2').data.include?('10.1.1.1')
+  end
+
+  # backwards compatibility, we always return a new string
+  def test_match_data_string
+    data = "hello"
+    t = Patricia.new
+    t.add('10.0.0.0/8', data)
+
+    z = t.match_best '10.1.1.1'
+    assert_equal "hello", z.data
+    assert(data.object_id != z.data.object_id)
+
+    y = t.match_best '10.1.1.1'
+    assert_equal "hello", y.data
+    assert(y.data.object_id != z.data.object_id)
+  end
+
+  def test_match_data_nothing
+    t = Patricia.new
+    t.add('10.0.0.0/8')
+
+    z = t.match_best '10.1.1.1'
+    assert_equal "", z.data
+    z_object_id = z.data
+
+    y = t.match_best '10.1.1.1'
+    assert_equal "", y.data
+    assert(y.data.object_id != z_object_id)
+  end
+
+end