File: memoized_input_test.rb

package info (click to toggle)
ruby-citrus 3.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 420 kB
  • sloc: ruby: 3,417; makefile: 5
file content (64 lines) | stat: -rw-r--r-- 1,153 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require File.expand_path('../helper', __FILE__)

class MemoizedInputTest < Test::Unit::TestCase
  def test_memoized?
    assert MemoizedInput.new('').memoized?
  end

  grammar :LetterA do
    rule :top do
      any(:three_as, :two_as, :one_a)
    end

    rule :three_as do
      rep(:one_a, 3, 3)
    end

    rule :two_as do
      rep(:one_a, 2, 2)
    end

    rule :one_a do
      "a"
    end
  end

  def test_cache_hits1
    input = MemoizedInput.new('a')
    input.exec(LetterA.rule(:top))
    assert_equal(3, input.cache_hits)
  end

  def test_cache_hits2
    input = MemoizedInput.new('aa')
    input.exec(LetterA.rule(:top))
    assert_equal(2, input.cache_hits)
  end

  def test_cache_hits3
    input = MemoizedInput.new('aaa')
    input.exec(LetterA.rule(:top))
    assert_equal(0, input.cache_hits)
  end
  
  grammar :LettersABC do
    rule :top do
      any(all(:a,:b,:c), all(:b,:a,:c), all(:b,:c,:a))
    end
    rule :a do
      "a"
    end
    rule :b do
      "b"
    end
    rule :c do
      "c"
    end
  end

  def test_memoization
    match = LettersABC.parse('bca',{:memoize=>true})
    assert_equal('bca',match)
  end 

end