From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001
From: Jari Vetoniemi <jari.vetoniemi@indooratlas.com>
Date: Mon, 16 Mar 2020 18:49:26 +0900
Subject: Fresh start

---
 jni/ruby/test/-ext-/st/test_foreach.rb | 15 ++++++++++
 jni/ruby/test/-ext-/st/test_numhash.rb | 49 +++++++++++++++++++++++++++++++++
 jni/ruby/test/-ext-/st/test_update.rb  | 50 ++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 jni/ruby/test/-ext-/st/test_foreach.rb
 create mode 100644 jni/ruby/test/-ext-/st/test_numhash.rb
 create mode 100644 jni/ruby/test/-ext-/st/test_update.rb

(limited to 'jni/ruby/test/-ext-/st')

diff --git a/jni/ruby/test/-ext-/st/test_foreach.rb b/jni/ruby/test/-ext-/st/test_foreach.rb
new file mode 100644
index 0000000..259b0a9
--- /dev/null
+++ b/jni/ruby/test/-ext-/st/test_foreach.rb
@@ -0,0 +1,15 @@
+require 'test/unit'
+require '-test-/st/foreach'
+
+class Test_StForeachUnpack < Test::Unit::TestCase
+  def test_st_foreach_check_unpack
+    assert_nil Bug.unp_st_foreach_check(:check), "goto unpacked_continue"
+    assert_nil Bug.unp_st_foreach_check(:delete1), "goto unpacked"
+    assert_nil Bug.unp_st_foreach_check(:delete2), "goto deleted"
+  end
+
+  def test_st_foreach_unpack
+    assert_nil Bug.unp_st_foreach(:unpacked), "goto unpacked"
+    assert_nil Bug.unp_st_foreach(:unpack_delete), "if (!ptr) return 0"
+  end
+end
diff --git a/jni/ruby/test/-ext-/st/test_numhash.rb b/jni/ruby/test/-ext-/st/test_numhash.rb
new file mode 100644
index 0000000..24dc87c
--- /dev/null
+++ b/jni/ruby/test/-ext-/st/test_numhash.rb
@@ -0,0 +1,49 @@
+require 'test/unit'
+require "-test-/st/numhash"
+
+class Bug::StNumHash
+  class Test_NumHash < Test::Unit::TestCase
+    def setup
+      @tbl = Bug::StNumHash.new
+      5.times {|i| @tbl[i] = i}
+    end
+
+    def test_check
+      keys = []
+      @tbl.each do |k, v, t|
+        keys << k
+        t[5] = 5 if k == 3
+        true
+      end
+      assert_equal([*0..5], keys)
+    end
+
+    def test_update
+      assert_equal(true, @tbl.update(0) {@tbl[5] = :x})
+      assert_equal(:x, @tbl[0])
+      assert_equal(:x, @tbl[5])
+    end
+
+    def test_size_after_delete_safe
+      10.downto(1) do |up|
+        tbl = Bug::StNumHash.new
+        1.upto(up){|i| tbl[i] = i}
+        assert_equal(1, tbl.delete_safe(1))
+        assert_equal(up - 1, tbl.size, "delete_safe doesn't change size from #{up} to #{up-1}")
+      end
+    end
+
+    def test_delete_safe_on_iteration
+      10.downto(1) do |up|
+        tbl = Bug::StNumHash.new
+        1.upto(up){|i| tbl[i] = i}
+        assert_nothing_raised("delete_safe forces iteration to fail with size #{up}") do
+          tbl.each do |k, v, t|
+            assert_equal k, t.delete_safe(k)
+            true
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/jni/ruby/test/-ext-/st/test_update.rb b/jni/ruby/test/-ext-/st/test_update.rb
new file mode 100644
index 0000000..1b41d2b
--- /dev/null
+++ b/jni/ruby/test/-ext-/st/test_update.rb
@@ -0,0 +1,50 @@
+require 'test/unit'
+require "-test-/st/update"
+
+class Bug::StTable
+  class Test_Update < Test::Unit::TestCase
+    def setup
+      @tbl = Bug::StTable.new
+      @tbl[:a] = 1
+      @tbl[:b] = 2
+    end
+
+    def test_notfound
+      assert_equal(false, @tbl.st_update(:c) {42})
+      assert_equal({a: 1, b: 2, c: 42}, @tbl)
+    end
+
+    def test_continue
+      args = nil
+      assert_equal(true, @tbl.st_update(:a) {|*x| args = x; false})
+      assert_equal({a: 1, b: 2}, @tbl, :a)
+      assert_equal([:a, 1], args)
+    end
+
+    def test_delete
+      args = nil
+      assert_equal(true, @tbl.st_update(:a) {|*x| args = x; nil})
+      assert_equal({b: 2}, @tbl, :a)
+      assert_equal([:a, 1], args)
+    end
+
+    def test_update
+      args = nil
+      assert_equal(true, @tbl.st_update(:a) {|*x| args = x; 3})
+      assert_equal({a: 3, b: 2}, @tbl, :a)
+      assert_equal([:a, 1], args)
+    end
+
+    def test_pass_objects_in_st_table
+      bug7330 = '[ruby-core:49220]'
+      key = "abc".freeze
+      value = "def"
+      @tbl[key] = value
+      @tbl.st_update("abc") {|*args|
+        assert_same(key, args[0], bug7330)
+        assert_same(value, args[1], bug7330)
+        nil
+      }
+    end
+  end
+end
-- 
cgit v1.2.3-70-g09d2