stamina 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.gemtest +0 -0
  2. data/CHANGELOG.md +22 -0
  3. data/Gemfile +2 -0
  4. data/Gemfile.lock +33 -0
  5. data/LICENCE.md +22 -0
  6. data/Manifest.txt +16 -0
  7. data/README.md +78 -0
  8. data/Rakefile +23 -0
  9. data/bin/adl2dot +12 -0
  10. data/bin/classify +12 -0
  11. data/bin/redblue +12 -0
  12. data/bin/rpni +12 -0
  13. data/example/adl/automaton.adl +49 -0
  14. data/example/adl/sample.adl +53 -0
  15. data/example/basic/characteristic_sample.adl +32 -0
  16. data/example/basic/target.adl +9 -0
  17. data/example/competition/31_test.adl +1500 -0
  18. data/example/competition/31_training.adl +1759 -0
  19. data/lib/stamina.rb +19 -0
  20. data/lib/stamina/adl.rb +298 -0
  21. data/lib/stamina/automaton.rb +1237 -0
  22. data/lib/stamina/automaton/walking.rb +336 -0
  23. data/lib/stamina/classifier.rb +37 -0
  24. data/lib/stamina/command/adl2dot_command.rb +73 -0
  25. data/lib/stamina/command/classify_command.rb +57 -0
  26. data/lib/stamina/command/redblue_command.rb +58 -0
  27. data/lib/stamina/command/rpni_command.rb +58 -0
  28. data/lib/stamina/command/stamina_command.rb +79 -0
  29. data/lib/stamina/errors.rb +20 -0
  30. data/lib/stamina/induction/commons.rb +170 -0
  31. data/lib/stamina/induction/redblue.rb +264 -0
  32. data/lib/stamina/induction/rpni.rb +188 -0
  33. data/lib/stamina/induction/union_find.rb +377 -0
  34. data/lib/stamina/input_string.rb +123 -0
  35. data/lib/stamina/loader.rb +0 -0
  36. data/lib/stamina/markable.rb +42 -0
  37. data/lib/stamina/sample.rb +190 -0
  38. data/lib/stamina/version.rb +14 -0
  39. data/stamina.gemspec +190 -0
  40. data/stamina.noespec +35 -0
  41. data/tasks/debug_mail.rake +78 -0
  42. data/tasks/debug_mail.txt +13 -0
  43. data/tasks/gem.rake +68 -0
  44. data/tasks/spec_test.rake +79 -0
  45. data/tasks/unit_test.rake +77 -0
  46. data/tasks/yard.rake +51 -0
  47. data/test/stamina/adl_test.rb +491 -0
  48. data/test/stamina/automaton_additional_test.rb +190 -0
  49. data/test/stamina/automaton_classifier_test.rb +155 -0
  50. data/test/stamina/automaton_test.rb +1092 -0
  51. data/test/stamina/automaton_to_dot_test.rb +64 -0
  52. data/test/stamina/automaton_walking_test.rb +206 -0
  53. data/test/stamina/exit.rb +3 -0
  54. data/test/stamina/induction/induction_test.rb +70 -0
  55. data/test/stamina/induction/redblue_mergesamestatebug_expected.adl +19 -0
  56. data/test/stamina/induction/redblue_mergesamestatebug_pta.dot +64 -0
  57. data/test/stamina/induction/redblue_mergesamestatebug_sample.adl +9 -0
  58. data/test/stamina/induction/redblue_test.rb +83 -0
  59. data/test/stamina/induction/redblue_universal_expected.adl +4 -0
  60. data/test/stamina/induction/redblue_universal_sample.adl +5 -0
  61. data/test/stamina/induction/rpni_inria_expected.adl +7 -0
  62. data/test/stamina/induction/rpni_inria_sample.adl +9 -0
  63. data/test/stamina/induction/rpni_test.rb +129 -0
  64. data/test/stamina/induction/rpni_test_pta.dot +22 -0
  65. data/test/stamina/induction/rpni_universal_expected.adl +4 -0
  66. data/test/stamina/induction/rpni_universal_sample.adl +4 -0
  67. data/test/stamina/induction/union_find_test.rb +124 -0
  68. data/test/stamina/input_string_test.rb +323 -0
  69. data/test/stamina/markable_test.rb +70 -0
  70. data/test/stamina/randdfa.adl +66 -0
  71. data/test/stamina/sample.adl +4 -0
  72. data/test/stamina/sample_classify_test.rb +149 -0
  73. data/test/stamina/sample_test.rb +218 -0
  74. data/test/stamina/small_dfa.dot +16 -0
  75. data/test/stamina/small_dfa.gif +0 -0
  76. data/test/stamina/small_nfa.dot +18 -0
  77. data/test/stamina/small_nfa.gif +0 -0
  78. data/test/stamina/stamina_test.rb +69 -0
  79. data/test/test_all.rb +7 -0
  80. metadata +279 -0
@@ -0,0 +1,70 @@
1
+ require 'test/unit'
2
+ require 'stamina/markable'
3
+ module Stamina
4
+ # Tests Loaded class
5
+ class MarkableTest < Test::Unit::TestCase
6
+
7
+ class MyMarkableObject
8
+ include Markable
9
+ def initialize
10
+ @data = {}
11
+ end
12
+ end
13
+
14
+ class MarkableObjectWithStateChanges < MyMarkableObject
15
+ def initialize
16
+ super
17
+ @changes = []
18
+ end
19
+
20
+ def state_changed(what, description)
21
+ @changes << what << description
22
+ end
23
+
24
+ def changes
25
+ @changes
26
+ end
27
+ end
28
+
29
+ # Tests the effect of setting a value
30
+ def test_value_assignments
31
+ @loaded = MyMarkableObject.new
32
+
33
+ assert_nil(@loaded[:myownkey])
34
+ assert_nil(@loaded["rr"])
35
+
36
+ @loaded[:myownkey] = "myownkey"
37
+ @loaded[12] = :twelve
38
+ assert_equal("myownkey", @loaded[:myownkey])
39
+ assert_equal(:twelve, @loaded[12])
40
+ assert_nil(@loaded["rr"])
41
+
42
+ @loaded[:myownkey] = 36
43
+ assert_equal(36, @loaded[:myownkey])
44
+ assert_equal(:twelve, @loaded[12])
45
+ assert_nil(@loaded["rr"])
46
+ end
47
+
48
+ # Tests the effect of setting a value
49
+ def test_value_assignments_statechange
50
+ @loaded = MarkableObjectWithStateChanges.new
51
+
52
+ assert_nil(@loaded[:myownkey])
53
+ assert_nil(@loaded["rr"])
54
+ assert_equal([],@loaded.changes)
55
+
56
+ @loaded[:myownkey] = "myownkey"
57
+ @loaded[12] = :twelve
58
+ assert_equal("myownkey", @loaded[:myownkey])
59
+ assert_equal(:twelve, @loaded[12])
60
+ assert_equal([:loaded_pair,[:myownkey,nil,"myownkey"],:loaded_pair,[12,nil,:twelve]],@loaded.changes)
61
+ assert_nil(@loaded["rr"])
62
+
63
+ @loaded[:myownkey] = 36
64
+ assert_equal(36, @loaded[:myownkey])
65
+ assert_equal(:twelve, @loaded[12])
66
+ assert_nil(@loaded["rr"])
67
+ assert_equal([:loaded_pair,[:myownkey,nil,"myownkey"],:loaded_pair,[12,nil,:twelve],:loaded_pair,[:myownkey,"myownkey",36]],@loaded.changes)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,66 @@
1
+ 26 39
2
+ 0 false false
3
+ 1 false true
4
+ 2 false true
5
+ 3 false true
6
+ 4 false false
7
+ 5 false true
8
+ 6 true false
9
+ 7 false false
10
+ 8 false true
11
+ 9 false true
12
+ 10 false false
13
+ 11 false false
14
+ 12 false false
15
+ 13 false true
16
+ 14 false true
17
+ 15 false false
18
+ 16 false false
19
+ 17 false false
20
+ 18 false true
21
+ 19 false true
22
+ 20 false false
23
+ 21 false true
24
+ 22 false true
25
+ 23 false false
26
+ 24 false true
27
+ 25 false true
28
+ 0 1 1
29
+ 0 2 0
30
+ 3 4 1
31
+ 3 5 0
32
+ 6 7 0
33
+ 6 8 1
34
+ 2 9 1
35
+ 2 10 0
36
+ 11 12 1
37
+ 11 13 0
38
+ 12 4 0
39
+ 12 12 1
40
+ 14 15 1
41
+ 14 16 0
42
+ 17 0 0
43
+ 18 19 0
44
+ 18 0 1
45
+ 20 1 1
46
+ 20 18 0
47
+ 21 18 0
48
+ 15 21 0
49
+ 15 22 1
50
+ 9 5 0
51
+ 22 17 1
52
+ 8 1 1
53
+ 8 20 0
54
+ 13 23 0
55
+ 13 5 1
56
+ 4 19 1
57
+ 24 11 0
58
+ 23 24 0
59
+ 23 3 1
60
+ 10 25 1
61
+ 10 24 0
62
+ 19 14 1
63
+ 25 7 1
64
+ 7 1 1
65
+ 7 24 0
66
+ 1 10 0
@@ -0,0 +1,4 @@
1
+ -
2
+ + a
3
+ - a b
4
+ + a b a
@@ -0,0 +1,149 @@
1
+ require 'test/unit'
2
+ require 'stamina/adl'
3
+ require 'stamina/stamina_test'
4
+ module Stamina
5
+ class Sample
6
+ # Tests Classify module, as installed on Sample and InputString classes.
7
+ class ClassifyTest < StaminaTest
8
+
9
+ # Tests Classify#correctly_classified_by? is correct on valid sample against
10
+ # small_dfa example
11
+ def test_valid_sample_correctly_classified_by_small_dfa
12
+ assert_equal(true, Sample.new.correctly_classified_by?(@small_dfa))
13
+ sample = ADL::parse_sample <<-SAMPLE
14
+ -
15
+ + b
16
+ + b c
17
+ - b c a
18
+ - b c a c
19
+ - b c a c a
20
+ - b c a a
21
+ + b c a b
22
+ + b c a b c a c b
23
+ - z
24
+ - b z
25
+ SAMPLE
26
+ assert_equal(true, sample.correctly_classified_by?(@small_dfa))
27
+ end
28
+
29
+ # Tests Classify#correctly_classified_by? is correct on invalid sample against
30
+ # small_dfa example
31
+ def test_invalid_sample_correctly_classified_by_small_dfa
32
+ sample = ADL::parse_sample <<-SAMPLE
33
+ -
34
+ + b
35
+ + b c
36
+ - b c a
37
+ # this one is reversed
38
+ + b c a c
39
+ - b c a c a
40
+ - b c a a
41
+ + b c a b
42
+ + b c a b c a c b
43
+ - z
44
+ - b z
45
+ SAMPLE
46
+ assert_equal(false, sample.correctly_classified_by?(@small_dfa))
47
+
48
+ sample = ADL::parse_sample <<-SAMPLE
49
+ -
50
+ + b
51
+ + b c
52
+ - b c a
53
+ - b c a c
54
+ - b c a c a
55
+ - b c a a
56
+ + b c a b
57
+ + b c a b c a c b
58
+ # this one is changed
59
+ + z
60
+ - b z
61
+ SAMPLE
62
+ assert_equal(false, sample.correctly_classified_by?(@small_dfa))
63
+
64
+ sample = ADL::parse_sample <<-SAMPLE
65
+ -
66
+ + b
67
+ + b c
68
+ - b c a
69
+ - b c a c
70
+ - b c a c a
71
+ - b c a a
72
+ + b c a b
73
+ # this one is changed
74
+ - b c a b c a c b
75
+ - z
76
+ - b z
77
+ SAMPLE
78
+ assert_equal(false, sample.correctly_classified_by?(@small_dfa))
79
+ end
80
+
81
+ # Tests Classify#correctly_classified_by? is correct on valid sample against
82
+ # small_nfa example
83
+ def test_valid_sample_correctly_classified_by_small_nfa
84
+ assert_equal(true, Sample.new.correctly_classified_by?(@small_nfa))
85
+ sample = ADL::parse_sample <<-SAMPLE
86
+ +
87
+ + a
88
+ - a a
89
+ + a a b
90
+ + a b
91
+ + a b c a
92
+ - a b c
93
+ + a b b b b b b
94
+ - a z
95
+ - z
96
+ SAMPLE
97
+ assert_equal(true, sample.correctly_classified_by?(@small_nfa))
98
+ end
99
+
100
+ # Tests Classify#correctly_classified_by? is correct on invalid sample against
101
+ # small_nfa example
102
+ def test_invalid_sample_correctly_classified_by_small_nfa
103
+ sample = ADL::parse_sample <<-SAMPLE
104
+ # this one is changed
105
+ -
106
+ + a
107
+ - a a
108
+ + a a b
109
+ + a b
110
+ + a b c a
111
+ - a b c
112
+ + a b b b b b b
113
+ - a z
114
+ - z
115
+ SAMPLE
116
+ assert_equal(false, sample.correctly_classified_by?(@small_nfa))
117
+ sample = ADL::parse_sample <<-SAMPLE
118
+ +
119
+ + a
120
+ - a a
121
+ + a a b
122
+ # this one is changed
123
+ - a b
124
+ + a b c a
125
+ - a b c
126
+ + a b b b b b b
127
+ - a z
128
+ - z
129
+ SAMPLE
130
+ assert_equal(false, sample.correctly_classified_by?(@small_nfa))
131
+ sample = ADL::parse_sample <<-SAMPLE
132
+ +
133
+ + a
134
+ # this one is changed
135
+ + a a
136
+ + a a b
137
+ + a b
138
+ + a b c a
139
+ - a b c
140
+ + a b b b b b b
141
+ - a z
142
+ - z
143
+ SAMPLE
144
+ assert_equal(false, sample.correctly_classified_by?(@small_nfa))
145
+ end
146
+
147
+ end # class ClassifyTest
148
+ end # class Sample
149
+ end # module Stamina
@@ -0,0 +1,218 @@
1
+ require 'test/unit'
2
+ require 'stamina/errors'
3
+ require 'stamina/stamina_test'
4
+ require 'stamina/sample'
5
+ module Stamina
6
+
7
+ # Tests Sample class
8
+ class SampleTest < StaminaTest
9
+
10
+ # Converts a String to an InputString
11
+ def s(str)
12
+ Stamina::ADL::parse_string(str)
13
+ end
14
+
15
+ # Tests Sample#empty?
16
+ def test_empty
17
+ assert_equal(true, Sample.new.empty?)
18
+ assert_equal(true, Sample[].empty?)
19
+ assert_equal(false, Sample['?'].empty?)
20
+ assert_equal(false, Sample['-'].empty?)
21
+ assert_equal(false, Sample['+'].empty?)
22
+ assert_equal(false, Sample['+ a b'].empty?)
23
+ assert_equal(false, Sample['+ a b', '- a'].empty?)
24
+ assert_equal(false, Sample['- a b'].empty?)
25
+ end
26
+
27
+ # Tests Sample#size
28
+ def test_size_and_counts
29
+ s = Sample.new
30
+ assert_equal(0, s.size)
31
+ assert_equal(0, s.positive_count)
32
+ assert_equal(0, s.negative_count)
33
+ s << '+ a b'
34
+ assert_equal(1, s.size)
35
+ assert_equal(1, s.positive_count)
36
+ assert_equal(0, s.negative_count)
37
+ s << '+ a b'
38
+ assert_equal(2, s.size)
39
+ assert_equal(2, s.positive_count)
40
+ assert_equal(0, s.negative_count)
41
+ s << '+ a'
42
+ assert_equal(3, s.size)
43
+ assert_equal(3, s.positive_count)
44
+ assert_equal(0, s.negative_count)
45
+ s << '- a b c'
46
+ assert_equal(4, s.size)
47
+ assert_equal(3, s.positive_count)
48
+ assert_equal(1, s.negative_count)
49
+ end
50
+
51
+ def test_same_string_can_be_added_many_times
52
+ s = Sample.new
53
+ 10.times {|i| s << "+ a b"}
54
+ assert_equal(10, s.size)
55
+ assert_equal(10, s.positive_count)
56
+ assert_equal(0, s.negative_count)
57
+ strings = s.collect{|s| s}
58
+ assert_equal 10, strings.size
59
+ end
60
+
61
+ # Tests Sample#<<
62
+ def test_append
63
+ s = Sample.new
64
+ assert_equal(s,s << '+',"Accepts empty string")
65
+ assert_equal(s,s << '+ a b a b a',"Accepts positive string")
66
+ assert_equal(s,s << '- a',"Accepts negative string")
67
+ assert_equal(s,s << '? a',"Accepts unlabeled string")
68
+ end
69
+
70
+ # Tests Sample#include? on every kind of arguments it announce
71
+ def test_append_accepts_arguments_it_annouce
72
+ expected = Sample[
73
+ '+ a b a b',
74
+ '+ a b',
75
+ '-',
76
+ '- a',
77
+ '+ a b a b a b'
78
+ ]
79
+ s = Sample.new
80
+ s << '+ a b a b'
81
+ s << ['+ a b', '-']
82
+ s << InputString.new('a', false)
83
+ s << Sample['+ a b a b a b', '-']
84
+ assert_equal(expected,s)
85
+ end
86
+
87
+ # Tests that Sample#<< detects inconsistencies
88
+ # def test_append_detects_inconsistency
89
+ # s = Sample.new
90
+ # s << '+ a b'
91
+ # s << '+ a b a b'
92
+ # assert_raise InconsistencyError do
93
+ # s << '- a b a b'
94
+ # end
95
+ # end
96
+
97
+ # Tests that Sample#<< detects inconsistencies
98
+ def test_append_detects_real_inconsistencies_only
99
+ s = Sample.new
100
+ s << '+ a b'
101
+ s << '+ a b a b'
102
+ assert_nothing_raised do
103
+ s << '- b'
104
+ s << '- a'
105
+ s << '- a b a'
106
+ end
107
+ end
108
+
109
+ # Tests each
110
+ def test_each
111
+ strings = ['+ a b a b', '+ a b', '+ a b', '- a', '+']
112
+ strings = strings.collect{|s| ADL::parse_string(s)}
113
+ s = Sample.new << strings
114
+ count = 0
115
+ s.each do |str|
116
+ assert_equal(true, strings.include?(str))
117
+ count += 1
118
+ end
119
+ assert_equal(strings.size, count)
120
+ end
121
+
122
+ # Tests each_positive
123
+ def test_each_positive
124
+ sample = Sample[
125
+ '+',
126
+ '- b',
127
+ '+ a b a b',
128
+ '- a b a a'
129
+ ]
130
+ count = 0
131
+ sample.each_positive do |str|
132
+ assert str.positive?
133
+ count += 1
134
+ end
135
+ assert_equal 2, count
136
+ positives = sample.positive_enumerator.collect{|s| s}
137
+ assert_equal 2, positives.size
138
+ [s('+'), s('+ a b a b')].each do |str|
139
+ assert positives.include?(str)
140
+ end
141
+ end
142
+
143
+ # Tests each_negative
144
+ def test_each_negative
145
+ sample = Sample[
146
+ '+',
147
+ '- b',
148
+ '+ a b a b',
149
+ '- a b a a'
150
+ ]
151
+ count = 0
152
+ sample.each_negative do |str|
153
+ assert str.negative?
154
+ count += 1
155
+ end
156
+ assert_equal 2, count
157
+ negatives = sample.negative_enumerator.collect{|s| s}
158
+ assert_equal 2, negatives.size
159
+ [s('- b'), s('- a b a a')].each do |str|
160
+ assert negatives.include?(str)
161
+ end
162
+ end
163
+
164
+ # Tests Sample#include?
165
+ def test_include
166
+ strings = ['+ a b a b', '+ a b', '- a', '+']
167
+ s = Sample.new << strings
168
+ strings.each do |str|
169
+ assert_equal(true, s.include?(str))
170
+ end
171
+ assert_equal(true, s.include?(strings))
172
+ assert_equal(true, s.include?(s))
173
+ assert_equal(false, s.include?('+ a'))
174
+ assert_equal(false, s.include?('-'))
175
+ assert_equal(false, s.include?('+ a b a'))
176
+ end
177
+
178
+ # Tests Sample#include? on every kind of arguments it announce
179
+ def test_include_accepts_arguments_it_annouce
180
+ s = Sample.new << ['+ a b a b', '+ a b', '- a', '+']
181
+ assert_equal true, s.include?('+ a b a b')
182
+ assert_equal true, s.include?(InputString.new('a b a b',true))
183
+ assert_equal true, s.include?(ADL::parse_string('+ a b a b'))
184
+ assert_equal true, s.include?(['+ a b a b', '+ a b'])
185
+ assert_equal true, s.include?(s)
186
+ end
187
+
188
+ # Tests Sample#==
189
+ def test_equal
190
+ s1 = Sample['+ a b a b', '+', '- a']
191
+ s2 = Sample['+ a b a b', '+', '+ a']
192
+ assert_equal(true, s1==s1)
193
+ assert_equal(true, s2==s2)
194
+ assert_equal(false, s1==s2)
195
+ assert_equal(false, s1==Sample.new)
196
+ assert_equal(false, s2==Sample.new)
197
+ end
198
+
199
+ # Test the signature
200
+ def test_signature
201
+ s = Sample.new
202
+ assert_equal '', s.signature
203
+ s = Sample.new << ['+ a b a b', '+ a b', '- a', '+']
204
+ assert_equal '1101', s.signature
205
+ s = Sample.new << ['+ a b a b', '+ a b', '- a', '?']
206
+ assert_equal '110?', s.signature
207
+ s = Stamina::ADL.parse_sample <<-SAMPLE
208
+ +
209
+ + a b
210
+ - a c
211
+ ? a d
212
+ SAMPLE
213
+ assert_equal '110?', s.signature
214
+ end
215
+
216
+ end # class SampleTest
217
+
218
+ end # module Stamina