Sha256: a2b33d4f02ffdab5bf822277b99b19b4078a3310bbd30941207a9e391f627117

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

#!/usr/bin/env ruby

require 'test/unit'
require 'tins'

module Tins
  class TestTinsMemoize < Test::Unit::TestCase
    class FooBar
      def foo(*a)
        @@foo ||= 0
        @@foo += 1
      end
      memoize_method :foo

      def bar(*a)
        @@bar ||= 0
        @@bar += 1
      end
      memoize_function :bar
    end

    def test_foo
      fb1 = FooBar.new
      fb2 = FooBar.new
      assert_equal 1, fb1.foo(1, 2)
      assert_equal 2, fb2.foo(1, 2)
      assert_equal 3, fb1.foo(1, 2, 3)
      assert_equal 4, fb2.foo(1, 2, 3)
      assert_equal 1, fb1.foo(1, 2)
      assert_equal 2, fb2.foo(1, 2)
      FooBar.memoize_cache_clear
      assert_equal 5, fb1.foo(1, 2)
      assert_equal 6, fb2.foo(1, 2)
      assert_equal 5, fb1.foo(1, 2)
      assert_equal 6, fb2.foo(1, 2)
    end

    def test_bar
      fb1 = FooBar.new
      fb2 = FooBar.new
      assert_equal 1, fb1.bar(1, 2)
      assert_equal 1, fb2.bar(1, 2)
      assert_equal 2, fb1.bar(1, 2, 3)
      assert_equal 2, fb2.bar(1, 2, 3)
      assert_equal 1, fb1.bar(1, 2)
      assert_equal 1, fb2.bar(1, 2)
      FooBar.memoize_cache_clear
      assert_equal 3, fb1.bar(1, 2)
      assert_equal 3, fb2.bar(1, 2)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tins-0.3.3 tests/tins_memoize_test.rb
tins-0.3.2 tests/tins_memoize_test.rb
tins-0.3.1 tests/tins_memoize_test.rb
tins-0.3.0 tests/tins_memoize_test.rb