Sha256: 903db02468cef85cf3c9e02c1c87215913f7ad21ee51a399013b6a43441fe63b

Contents?: true

Size: 1.58 KB

Versions: 28

Compression:

Stored size: 1.58 KB

Contents

require 'test_helper'
require 'tins'

module Tins
  class MemoizeTest < 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_method_cache
      fb1 = FooBar.new
      fb2 = FooBar.new
      assert_equal true, fb1.__memoize_cache__.empty?
      assert_equal true, fb2.__memoize_cache__.empty?
      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)
      fb1.memoize_cache_clear
      fb2.memoize_cache_clear
      assert_equal true, fb1.__memoize_cache__.empty?
      assert_equal true, fb2.__memoize_cache__.empty?
      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)
      assert_equal false, fb1.__memoize_cache__.empty?
      assert_equal false, fb2.__memoize_cache__.empty?
    end

    def test_function_cache
      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)
      assert_equal false, FooBar.__memoize_cache__.empty?
    end
  end
end

Version data entries

28 entries across 27 versions & 3 rubygems

Version Path
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/tests/memoize_test.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/tests/memoize_test.rb
tdiary-5.0.8 vendor/bundle/gems/tins-0.13.2/tests/memoize_test.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/tins-0.13.2/tests/memoize_test.rb
tins-1.3.1 tests/memoize_test.rb
tins-1.3.0 tests/memoize_test.rb
tins-1.2.0 tests/memoize_test.rb
tins-1.1.0 tests/memoize_test.rb
tins-1.0.1 tests/memoize_test.rb
tins-1.0.0 tests/memoize_test.rb
tins-0.13.2 tests/memoize_test.rb
social_url_stats-0.0.1 vendor/ruby/1.9.1/gems/tins-0.13.1/tests/memoize_test.rb
tins-0.13.1 tests/memoize_test.rb
tins-0.13.0 tests/memoize_test.rb
tins-0.12.0 tests/memoize_test.rb
tins-0.11.0 tests/memoize_test.rb
tins-0.10.0 tests/memoize_test.rb
tins-0.9.0 tests/memoize_test.rb
tins-0.8.4 tests/memoize_test.rb
tins-0.8.3 tests/memoize_test.rb