galaxy 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/.idea/.rakeTasks +7 -0
  4. data/.idea/encodings.xml +5 -0
  5. data/.idea/galaxy.iml +10 -0
  6. data/.idea/misc.xml +14 -0
  7. data/.idea/modules.xml +9 -0
  8. data/.idea/vcs.xml +8 -0
  9. data/.idea/workspace.xml +486 -0
  10. data/LICENSE +20 -0
  11. data/README.rdoc +36 -0
  12. data/Rakefile +62 -0
  13. data/VERSION +1 -0
  14. data/doc/plan.txt +5 -0
  15. data/doc/pseudo.txt +43 -0
  16. data/features/galaxy.feature +9 -0
  17. data/features/step_definitions/galaxy_steps.rb +0 -0
  18. data/features/support/env.rb +4 -0
  19. data/galaxy.gemspec +95 -0
  20. data/galaxy/.loadpath +5 -0
  21. data/galaxy/.project +17 -0
  22. data/galaxy/.settings/org.eclipse.mylyn.tasks.ui.prefs +4 -0
  23. data/galaxy/.settings/org.eclipse.wst.sse.core.prefs +5 -0
  24. data/galaxy/experiments.rb +26 -0
  25. data/lib/galaxy.rb +8 -0
  26. data/lib/galaxy/models/bombing.rb +64 -0
  27. data/lib/galaxy/models/fleet.rb +62 -0
  28. data/lib/galaxy/models/group.rb +178 -0
  29. data/lib/galaxy/models/models.rb +16 -0
  30. data/lib/galaxy/models/planet.rb +181 -0
  31. data/lib/galaxy/models/product.rb +84 -0
  32. data/lib/galaxy/models/race.rb +112 -0
  33. data/lib/galaxy/models/route.rb +60 -0
  34. data/lib/galaxy/order.rb +24 -0
  35. data/lib/galaxy/report.rb +176 -0
  36. data/lib/galaxy/section.rb +226 -0
  37. data/lib/galaxy/utils.rb +109 -0
  38. data/lib/galaxy/virtual_base.rb +165 -0
  39. data/spec/spec_helper.rb +9 -0
  40. data/test/test_helper.rb +4 -0
  41. data/test/unit/models_test.rb +1469 -0
  42. data/test/unit/report_test.rb +187 -0
  43. data/test/unit/utils_test.rb +421 -0
  44. data/test/unit/virtual_base_test.rb +224 -0
  45. metadata +123 -0
@@ -0,0 +1,187 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ #require 'report.rb'
3
+
4
+ class X_ReportTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ end
8
+
9
+ def test_rep_081
10
+
11
+ #Open Report
12
+ start = Time.now
13
+ rep = Report.new "rep/ArVit081.rep"
14
+
15
+ printf "Length: #{rep.text.length} "
16
+ assert_equal 491978, rep.text.length, 'File length is wrong'
17
+ printf "#{Time.now} Elapsed: #{Time.now-start}\n"
18
+ assert Time.now-start < 0.1, 'Report is loading too long'
19
+
20
+ # Parse Report (possibly many times)
21
+ start = Time.now
22
+ 1.times do rep.parse end
23
+
24
+ printf "#{Time.now} Elapsed: #{Time.now-start}\n"
25
+ assert Time.now-start < 20, 'Report is parsing too long'
26
+
27
+ puts rep.status
28
+
29
+ p rep.races[5], rep.sciences[5], rep.designs[5], rep.battle_groups[5], rep.bombings[0], rep.incoming_groups[0],
30
+ rep.your_planets[5], rep.planets[145], rep.unidentified_planets[5], rep.uninhabited_planets[5],
31
+ rep.routes[0], rep.fleets[0], rep.groups[5], rep.your_groups[5], rep.unidentified_groups[5]
32
+ # rep.designs.each {|d| p d}
33
+ #p rep.groups.find_all {|p| p.nil?}.size
34
+ #p rep.planets.find {|p| p and not p.idx}
35
+ #p rep.races['ArVitallian'].planets.size, rep.your_planets - rep.races['ArVitallian'].planets
36
+ #rep.your_planets.each{|elt| print '.' ; s = rep.your_planets.find_all{|e| e==elt}.size; p s, elt if s>1}
37
+ #rep.your_groups.each{|g| p g if g.from_battle?}
38
+ #p rep.groups.find_all{|g| !g.from_battle? and !g.incoming? and !g.unknown?}.size
39
+ #p rep.races.inject(0){|total, race| puts "#{race.name} #{race.groups.size} #{race.battle_groups.size} + #{total}" if race.groups.size > 0 ; total + race.groups.size}
40
+
41
+
42
+ # Assert report stats
43
+ assert_equal 'ArVitallian', rep.owner
44
+ assert_equal 'research3', rep.game
45
+ assert_equal 81, rep.turn
46
+ assert_equal 'Tue Aug 30 03:59:01 2005', rep.time
47
+ assert_equal 'Galaxy PLUS version 1.7 - Dragon Galaxy (NG-edition) 3.01', rep.server
48
+ assert_equal 140, rep.races.size #confirmed
49
+ assert_equal 160, rep.designs.size #confirmed
50
+ assert_equal 2, rep.bombings.size #confirmed
51
+ assert_equal 8, rep.fleets.size #confirmed
52
+ assert_equal 2, rep.routes.size #confirmed
53
+ assert_equal 1582,rep.sciences.size #confirmed
54
+ assert_equal 733, rep.planets.size #confirmed
55
+ assert_equal 354, rep.your_planets.size #confirmed
56
+ assert_equal 70, rep.uninhabited_planets.size #confirmed
57
+ assert_equal 144, rep.unidentified_planets.size #confirmed
58
+ assert_equal 29, rep.incoming_groups.size #confirmed
59
+ assert_equal 111, rep.unknown_groups.size #confirmed 82 "unidentified" + 29 "incoming"
60
+ assert_equal 96, rep.battle_groups.size #confirmed
61
+ assert_equal 1399, rep.your_active_groups.size #confirmed
62
+ assert_equal 1453, rep.your_groups.size #confirmed
63
+ assert_equal 2535+96+82+29, rep.groups.size #sum(1)=2535(*Groups)+96(Battle Groups)+82(unidentified)+29(incoming)
64
+ assert_equal 10288, rep.planets.find_all{|p| p.num}.max{|p1,p2|p1.num<=>p2.num}.num # Max planet number
65
+
66
+ # Asserting Race collections completeness
67
+ print 'Race collections.'
68
+ rep.races.each do |race|
69
+ next if race.rip?
70
+ print "d"
71
+ assert_equal race.products, rep.products.find_all {|d| d.race==race}, 'Designs Collection broken'
72
+ print "p"
73
+ assert_equal race.planets.sort, rep.planets.find_all {|p| p and p.race==race}.sort, 'Planets Collection broken'
74
+ print "r"
75
+ assert_equal race.routes.sort, rep.routes.find_all {|p| p and p.race==race}.sort, 'Routes Collection broken'
76
+ print "f"
77
+ assert_equal race.fleets.sort, rep.fleets.find_all {|p| p and p.race==race}.sort, 'Fleets Collection broken'
78
+ print "b"
79
+ assert_equal race.bombings.sort, rep.bombings.find_all {|p| p and p.race==race}.sort, 'Bombings Collection broken'
80
+ print "g"
81
+ assert_equal race.groups.sort, rep.groups.find_all {|p| p and p.race==race}.sort, 'Groups Collection broken'
82
+ print "."
83
+ end
84
+
85
+ # Assert individual gamedata elements
86
+ #p rep.planets[0]
87
+ #p rep.races['Mutabor']
88
+
89
+ # p rep.groups.select{|g| g.race=='Homo_galaktikus'}.size
90
+
91
+ p rep.designs.find_all {|d| d.race==rep.races['CBuHbu']}
92
+ p rep.races['CBuHbu'].designs
93
+
94
+ # Test selection on data collections
95
+ #sum =0
96
+ #drons = rep.your_groups.select {|group| group[13].to_i == 1 and group[1].to_i == 1}
97
+ #drons += rep.groups.select {|group| group[10].to_i == 1 and group[0].to_i == 1}
98
+ #transports = rep.your_groups.select {|group| group[6].to_f != 0 }
99
+ #transports += rep.groups.select {|group| group[5].to_f != 0 }
100
+
101
+ # Test cycles on data collections
102
+ #transports.each do |group|
103
+ # rep.planets.select {|planet| planet[3] == group[8]}
104
+ # rep.planets.each do |planet|
105
+ # sum += 1 if planet[3] == group[8]
106
+ # end
107
+ #end
108
+
109
+ #printf "Transports: #{transports.size} Drons: #{drons.size} "
110
+
111
+ printf " #{Time.now} Elapsed: #{Time.now-start}\n"
112
+
113
+ end
114
+ def no_test_rep_187
115
+
116
+ #Open Report
117
+ start = Time.now
118
+ rep = Report.new "rep/ArVit187.rep"
119
+
120
+ printf "Length: #{rep.text.length} "
121
+ assert_equal 6025330, rep.text.length, 'File length is wrong'
122
+
123
+ printf "#{Time.now} Elapsed: #{Time.now-start}\n"
124
+ assert Time.now-start < 0.3, 'Report is loading too long'
125
+
126
+ # Parse Report (possibly many times)
127
+ start = Time.now
128
+ 1.times do rep.parse end
129
+
130
+ printf "#{Time.now} Elapsed: #{Time.now-start}\n"
131
+ assert Time.now-start < 130, 'Report is parsing too long'
132
+
133
+ puts rep.status
134
+
135
+ p rep.races[5], rep.sciences[5], rep.designs[5], rep.battle_groups[5], rep.bombings[0], rep.incoming_groups[0],
136
+ rep.your_planets[5], rep.planets[145], rep.unidentified_planets[5], rep.uninhabited_planets[5],
137
+ rep.routes[0], rep.fleets[0], rep.groups[5], rep.your_groups[5], rep.unidentified_groups[5]
138
+
139
+ # Assert report stats
140
+ assert_equal 'ArVitallian', rep.owner
141
+ assert_equal 'research3', rep.game
142
+ assert_equal 187, rep.turn
143
+ assert_equal 'Mon Oct 09 21:19:05 2006', rep.time
144
+ assert_equal 'Galaxy PLUS version 1.7 - Dragon Galaxy (NG-edition) 3.04', rep.server
145
+ assert_equal 140, rep.races.size #confirmed
146
+ assert_equal 9488,rep.sciences.size #confirmed
147
+ assert_equal 1544, rep.designs.size #confirmed
148
+ assert_equal 24, rep.bombings.size #confirmed
149
+ assert_equal 2515, rep.your_planets.size #confirmed
150
+ assert_equal 149, rep.uninhabited_planets.size #confirmed
151
+ assert_equal 956, rep.unidentified_planets.size #confirmed
152
+ assert_equal 6046, rep.planets.size #confirmed #sum(1)=2535 ???xxx(*Groups)+96(Battle Groups)+82(unidentified)+29(incoming)
153
+ assert_equal 10292, rep.planets.find_all{|p| p.num}.max{|p1,p2|p1.num<=>p2.num}.num # Max planet number
154
+ assert_equal 16, rep.fleets.size #confirmed
155
+ assert_equal 26, rep.routes.size #confirmed
156
+ assert_equal 3203, rep.battle_groups.size #confirmed
157
+ assert_equal 769, rep.unknown_groups.size #confirmed ??? d 593 "unidentified" + 176 "incoming"
158
+ assert_equal 176, rep.incoming_groups.size #confirmed
159
+ assert_equal 9864, rep.your_active_groups.size #confirmed
160
+ assert_equal 10406, rep.your_groups.size #confirmed 9864 active + 542 from battle
161
+ assert_equal 39348, rep.groups.size #sum(1)=37018
162
+ #Report: ArVitallian research3 187 Mon Oct 09 21:19:05 2006 Galaxy PLUS version 1.7 - Dragon Galaxy (NG-edition) 3.04
163
+ #Races: 140 Sciences: 9488 Types: 1544 BattleGroups: 3203 Bombings: 24 Incomings: 176 Your Planets: 2515 Ships in Production: Routes: 26
164
+ #Planets: 6046 Uninhabited Planets: 149 Unidentified Planets: 956 Fleets: 16 Your Groups: 10406 Groups: 39348 Unidentified Groups: 769
165
+
166
+ # Asserting Race collections completeness
167
+ print 'Race collections.'
168
+ rep.races.each do |race|
169
+ next if race.rip?
170
+ print "d"
171
+ assert_equal race.products, rep.products.find_all {|d| d.race==race}, 'Designs Collection broken'
172
+ print "p"
173
+ assert_equal race.planets.sort, rep.planets.find_all {|p| p and p.race==race}.sort, 'Planets Collection broken'
174
+ print "r"
175
+ assert_equal race.routes.sort, rep.routes.find_all {|p| p and p.race==race}.sort, 'Routes Collection broken'
176
+ print "f"
177
+ assert_equal race.fleets.sort, rep.fleets.find_all {|p| p and p.race==race}.sort, 'Fleets Collection broken'
178
+ print "b"
179
+ assert_equal race.bombings.sort, rep.bombings.find_all {|p| p and p.race==race}.sort, 'Bombings Collection broken'
180
+ print "g"
181
+ assert_equal race.groups.sort, rep.groups.find_all {|p| p and p.race==race}.sort, 'Groups Collection broken'
182
+ print "."
183
+ end
184
+
185
+ printf " #{Time.now} Elapsed: #{Time.now-start}\n"
186
+ end
187
+ end
@@ -0,0 +1,421 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ #require 'utils.rb'
3
+
4
+ class A_ConstKeysHATest < Test::Unit::TestCase
5
+
6
+ class TestObj
7
+ attr_reader :key
8
+ attr_accessor :idx, :data
9
+
10
+ def initialize key = nil, idx = nil, data = "Empty"
11
+ @key = key; @idx = idx; @data = data
12
+ end
13
+ end
14
+
15
+ def setup
16
+ @o1 = TestObj.new('key1')
17
+ @o2 = TestObj.new('key2', 2, 'Data 2')
18
+ @o3 = TestObj.new('key3', 3, 'Data 3')
19
+ @o7 = TestObj.new('key7', 7, 'Data 7')
20
+
21
+ @o10 = TestObj.new('key10', nil, 'Data 10')
22
+ @o11 = TestObj.new('key11', nil, 'Data 11')
23
+ @o12 = TestObj.new('key12', 12, 'Data 12')
24
+ @o13 = TestObj.new(nil, nil, 'Data 13')
25
+ @o14 = TestObj.new('double_key', nil, 'Data 14')
26
+ @o15 = TestObj.new('key15', 15, 'Data 15')
27
+
28
+ @b = HashArray.new
29
+ @a = HashArray[0, 1, 2, @o7, @o1, @o3, @o2]
30
+ @hm = {:key2=>2, :key3=>3, :key7=>7, :key1=>8}
31
+ end
32
+
33
+ def test_create
34
+ assert_equal [], HashArray.new, "Empty array creation failed"
35
+ assert_equal [1,2,3,4,5], HashArray[1,2,3,4,5], "Literal array creation failed"
36
+ assert_equal [1,1,1,1,1], HashArray.new(5,1), "Repeated array creation failed"
37
+ assert_equal [0, 1, @o2, @o3, nil, nil, nil, @o7, @o1], @a, "Literal Object array creation failed"
38
+ end
39
+
40
+ def test_delete
41
+ @a >> 1
42
+ assert_nil @a[1], 'Deleted element not nil'
43
+ @a >> @o1
44
+ assert_nil @a[8], 'Deleted element not nil'
45
+ assert_nil @a[:key1], 'Deleted element not nil'
46
+
47
+ # Deleting element that is not in Array throws an exception
48
+ assert_raise ArgumentError do @a >> @o11 end
49
+
50
+ assert_equal @a, @a << @o11 >> @o11, 'Round-trip adding and removing fails'
51
+ end
52
+
53
+ def test_access
54
+ assert_equal @hm, @a.hashmap, "Hashmap matching failed"
55
+ assert_nil @a[44], 'Out-of-range element not nil'
56
+ assert_nil @a[nil], 'Nil index element not nil'
57
+ assert_equal @o7, @a[7], 'Integer index access failed'
58
+ assert_equal @o1, @a[-1], 'Negative integer index access failed'
59
+ assert_equal @o3, @a['key3'], 'String index access failed'
60
+ assert_equal [1, @o2, @o3, nil], @a[1..4], 'Range access failed'
61
+ assert_equal [1, @o2, @o3], @a[1,3], 'Slice access failed'
62
+ end
63
+
64
+ def test_simple_assign
65
+ @a[] = 'end'
66
+ assert_equal 'end', @a[9], 'No-index assign failed'
67
+ @a << 'more_end'
68
+ assert_equal 'more_end', @a[10], 'Addition failed'
69
+
70
+ @a[4] = 11
71
+ assert_equal 11, @a[4], 'Index assign failed'
72
+ @a[4] = 22
73
+ assert_equal 22, @a[4], 'Index re-assign failed'
74
+
75
+ @a['key'] = 33
76
+ assert_equal 33, @a['key'], 'Key assign failed'
77
+
78
+ @a[5,'double_key'] = 'double'
79
+ assert_equal 'double', @a['double_key'], 'Index/key assign failed'
80
+ assert_equal 'double', @a[5], 'Index/key assign failed'
81
+
82
+ @a[11] = 'replace'
83
+ assert_nil @a['key'], 'Keyed value replacement failed'
84
+ assert_nil @a.hashmap['key'], 'Keyed value replacement failed'
85
+
86
+ @a['double_key'] = 'trouble'
87
+ assert_equal 'trouble', @a['double_key'], 'Key reuse failed'
88
+ assert_nil @a[5], 'Key reuse failed'
89
+
90
+ @a[25,'out_key'] = 'outlier'
91
+ assert_equal 'outlier', @a['out_key'], 'Out-of-range index/key assign failed'
92
+ assert_equal 'outlier', @a[25], 'Out-of-range index/key assign failed'
93
+ end
94
+
95
+ def test_object_assign
96
+ @a[] = @o11
97
+ assert_equal @o11, @a[9], 'No-index assign failed'
98
+ assert_equal @o11, @a['key11'], 'No-index assign failed'
99
+ assert_equal 9, @a[9].idx, 'No-index assign failed'
100
+
101
+ @a << @o10
102
+ assert_equal @o10, @a[10], 'Addition failed'
103
+ assert_equal @o10, @a['key10'], 'Addition failed'
104
+ assert_equal 10, @a[10].idx, 'Addition failed'
105
+
106
+ # Add to Array object that is already there
107
+ @a[] = @o3
108
+ assert_equal @o3, @a[3], 'Object reassign failed'
109
+ assert_equal @o3, @a['key3'], 'Object reassign failed'
110
+ assert_equal 3, @a[3].idx, 'Object reassign failed'
111
+
112
+ # Assign object by index and replace element later
113
+ @a[4] = @o12
114
+ assert_equal @o12, @a[4], 'Index assign failed'
115
+ assert_equal @o12, @a['key12'], 'Index assign failed'
116
+ assert_equal 4, @a[4].idx, 'Index assign failed'
117
+ @a[4] = 22
118
+ assert_equal 22, @a[4], 'Index re-assign failed'
119
+ assert_nil @a['key12'], 'Index re-assign failed'
120
+ assert_nil @o12.idx, 'Index re-assign failed'
121
+ assert_equal 'key12', @o12.key, 'Index re-assign failed'
122
+
123
+ # Assign object by key
124
+ @a['key'] = @o12
125
+ assert_equal @o12, @a['key'], 'Key assign failed'
126
+ assert_equal 11, @o12.idx, 'Key assign failed'
127
+
128
+ # Assign object by index and key
129
+ @a[5,'double_key'] = @o13
130
+ assert_equal @o13, @a['double_key'], 'Index/key assign failed'
131
+ assert_equal @o13, @a[5], 'Index/key assign failed'
132
+ assert_equal 5, @o13.idx, 'Index/key assign failed'
133
+
134
+ # Replace object by index and key with another object (different key)
135
+ @a[5,'new_key'] = @o2
136
+ assert_equal @o2, @a['new_key'], 'Index/key replacement assign failed'
137
+ assert_equal @o2, @a[5], 'Index/key replacement assign failed'
138
+ assert_equal 5, @o2.idx, 'Index/key replacement assign failed'
139
+ assert_nil @a['double_key'], 'Index/key replacement assign failed'
140
+ assert_nil @o13.idx, 'Index/key replacement assign failed'
141
+
142
+ # Replace object by index and key with another object (same key)
143
+ @a[5,'new_key'] = @o1
144
+ assert_equal @o1, @a['new_key'], 'Index/key replacement assign failed'
145
+ assert_equal @o1, @a[5], 'Index/key replacement assign failed'
146
+ assert_equal 5, @o1.idx, 'Index/key replacement assign failed'
147
+ assert_nil @o2.idx, 'Index/key replacement assign failed'
148
+
149
+ # Replace object by index (new object has different key)
150
+ @a[5] = @o13
151
+ assert_nil @a['new_key'], 'Keyed value replacement failed'
152
+ assert_nil @a.hashmap['new_key'], 'Keyed value replacement failed'
153
+ assert_nil @o1.idx, 'Keyed value replacement failed'
154
+
155
+ # Replace object by index (new object has same key)
156
+ @a[5] = @o14
157
+ assert_equal 'double_key', @o14.key, 'Keyed value replacement failed'
158
+ assert_nil @o13.key, 'Keyed value replacement failed' #old key value is deleted
159
+
160
+ # Assign new key to element already in HashArray
161
+ @a['double_key'] = @o2
162
+ assert_equal @o2, @a['double_key'], 'Key reuse failed'
163
+ assert_equal @o2, @a[2], 'Key reuse failed'
164
+ assert_nil @a[5], 'Key reuse failed'
165
+
166
+ # Assign to element beyond HashArray border
167
+ @a[25,'out_key'] = @o15
168
+ assert_equal @o15, @a['out_key'], 'Out-of-range index/key assign failed'
169
+ assert_equal @o15, @a[25], 'Out-of-range index/key assign failed'
170
+ assert_equal 25, @o15.idx, 'Index/key replacement assign failed'
171
+ end
172
+ end
173
+
174
+ class A_MutableKeysHATest < Test::Unit::TestCase
175
+
176
+ class TestObj
177
+ attr_accessor :key, :idx, :data
178
+
179
+ def initialize key = nil, idx = nil, data = "Empty"
180
+ @key = key; @idx = idx; @data = data
181
+ end
182
+ end
183
+
184
+ def setup
185
+ @o1 = TestObj.new('key1')
186
+ @o2 = TestObj.new('key2', 2, 'Data 2')
187
+ @o3 = TestObj.new('key3', 3, 'Data 3')
188
+ @o7 = TestObj.new('key7', 7, 'Data 7')
189
+
190
+ @o10 = TestObj.new('key10', nil, 'Data 10')
191
+ @o11 = TestObj.new('key11', nil, 'Data 11')
192
+ @o12 = TestObj.new('key12', 12, 'Data 12')
193
+ @o13 = TestObj.new(nil, nil, 'Data 13')
194
+ @o14 = TestObj.new('double_key', nil, 'Data 14')
195
+ @o15 = TestObj.new('key15', 15, 'Data 15')
196
+ @o16 = TestObj.new('key16', 16, 'Data 16')
197
+
198
+ @b = HashArray.new
199
+ @a = HashArray[0, 1, 2, @o7, @o1, @o3, @o2]
200
+ @hm = {:key2=>2, :key3=>3, :key7=>7, :key1=>8}
201
+ end
202
+
203
+ def test_create
204
+ assert_equal [], HashArray.new, "Empty array creation failed"
205
+ assert_equal [1,2,3,4,5], HashArray[1,2,3,4,5], "Literal array creation failed"
206
+ assert_equal [1,1,1,1,1], HashArray.new(5,1), "Repeated array creation failed"
207
+ assert_equal [0, 1, @o2, @o3, nil, nil, nil, @o7, @o1], @a, "Literal Object array creation failed"
208
+ end
209
+
210
+ def test_delete
211
+ @a >> 1
212
+ assert_nil @a[1], 'Deleted element not nil'
213
+ @a >> @o1
214
+ assert_nil @a[8], 'Deleted element not nil'
215
+ assert_nil @a[:key1], 'Deleted element not nil'
216
+
217
+ # Deleting element that is not in Array throws an exception
218
+ assert_raise ArgumentError do @a >> @o11 end
219
+ assert_equal @a, @a << @o11 >> @o11, 'Round-trip adding and removing fails'
220
+ end
221
+
222
+ def test_access
223
+ assert_equal @hm, @a.hashmap, "Hashmap matching failed"
224
+ assert_nil @a[44], 'Out-of-range element not nil'
225
+ assert_nil @a[nil], 'Nil index element not nil'
226
+ assert_equal @o7, @a[7], 'Integer index access failed'
227
+ assert_equal @o1, @a[-1], 'Negative integer index access failed'
228
+ assert_equal @o3, @a['key3'], 'String index access failed'
229
+ assert_equal [1, @o2, @o3, nil], @a[1..4], 'Range access failed'
230
+ assert_equal [1, @o2, @o3], @a[1,3], 'Slice access failed'
231
+ end
232
+
233
+ def test_simple_assign
234
+ @a[] = 'end'
235
+ assert_equal 'end', @a[9], 'No-index assign failed'
236
+ @a << 'more_end'
237
+ assert_equal 'more_end', @a[10], 'Addition failed'
238
+
239
+ @a[4] = 11
240
+ assert_equal 11, @a[4], 'Index assign failed'
241
+ @a[4] = 22
242
+ assert_equal 22, @a[4], 'Index re-assign failed'
243
+
244
+ @a['key'] = 33
245
+ assert_equal 33, @a['key'], 'Key assign failed'
246
+ assert_equal 33, @a[:key], 'Key assign failed'
247
+
248
+ @a[5,'double_key'] = 'double'
249
+ assert_equal 'double', @a['double_key'], 'Index/key assign failed'
250
+ assert_equal 'double', @a[5], 'Index/key assign failed'
251
+
252
+ @a[11] = 'replace'
253
+ assert_nil @a['key'], 'Keyed value replacement failed'
254
+ assert_nil @a.hashmap['key'], 'Keyed value replacement failed'
255
+
256
+ @a['double_key'] = 'trouble'
257
+ assert_equal 'trouble', @a['double_key'], 'Key reuse failed'
258
+ assert_nil @a[5], 'Key reuse failed'
259
+
260
+ @a[25,'out_key'] = 'outlier'
261
+ assert_equal 'outlier', @a['out_key'], 'Out-of-range index/key assign failed'
262
+ assert_equal 'outlier', @a[25], 'Out-of-range index/key assign failed'
263
+ end
264
+
265
+ def test_object_assign
266
+ @a[] = @o11
267
+ assert_equal @o11, @a[9], 'No-index assign failed'
268
+ assert_equal @o11, @a['key11'], 'No-index assign failed'
269
+ assert_equal 9, @a[9].idx, 'No-index assign failed'
270
+
271
+ @a << @o10
272
+ assert_equal @o10, @a[10], 'Addition failed'
273
+ assert_equal @o10, @a['key10'], 'Addition failed'
274
+ assert_equal 10, @a[10].idx, 'Addition failed'
275
+
276
+ # Add to Array object that is already there
277
+ @a[] = @o3
278
+ assert_equal @o3, @a[3], 'Object reassign failed'
279
+ assert_equal @o3, @a['key3'], 'Object reassign failed'
280
+ assert_equal 3, @a[3].idx, 'Object reassign failed'
281
+
282
+ # Assign object by index and replace element later
283
+ @a[4] = @o12
284
+ assert_equal @o12, @a[4], 'Index assign failed'
285
+ assert_equal @o12, @a['key12'], 'Index assign failed'
286
+ assert_equal 4, @a[4].idx, 'Index assign failed'
287
+ @a[4] = 22
288
+ assert_equal 22, @a[4], 'Index re-assign failed'
289
+ assert_nil @a['key12'], 'Index re-assign failed'
290
+ assert_nil @o12.idx, 'Index re-assign failed'
291
+ assert_nil @o12.key, 'Index re-assign failed'
292
+
293
+ # Assign object by key
294
+ @a['key'] = @o12
295
+ assert_equal @o12, @a['key'], 'Key assign failed'
296
+ assert_equal :key, @o12.key, 'Key assign failed'
297
+ assert_equal 11, @o12.idx, 'Key assign failed'
298
+
299
+ # Assign object by index and key
300
+ @a[5,'double_key'] = @o13
301
+ assert_equal @o13, @a['double_key'], 'Index/key assign failed'
302
+ assert_equal @o13, @a[5], 'Index/key assign failed'
303
+ assert_equal :double_key, @o13.key, 'Index/key assign failed'
304
+ assert_equal 5, @o13.idx, 'Index/key assign failed'
305
+
306
+ # Replace object by index and key with another object (different key)
307
+ @a[5,'new_key'] = @o2
308
+ assert_equal @o2, @a['new_key'], 'Index/key replacement assign failed'
309
+ assert_equal @o2, @a[5], 'Index/key replacement assign failed'
310
+ assert_equal :new_key, @o2.key, 'Index/key replacement assign failed'
311
+ assert_equal 5, @o2.idx, 'Index/key replacement assign failed'
312
+ assert_nil @a['double_key'], 'Index/key replacement assign failed'
313
+ assert_nil @o13.key, 'Index/key replacement assign failed'
314
+ assert_nil @o13.idx, 'Index/key replacement assign failed'
315
+
316
+ # Replace object by index and key with another object (same key)
317
+ @a[5,'new_key'] = @o1
318
+ assert_equal @o1, @a['new_key'], 'Index/key replacement assign failed'
319
+ assert_equal @o1, @a[5], 'Index/key replacement assign failed'
320
+ assert_equal :new_key, @o1.key, 'Index/key replacement assign failed'
321
+ assert_equal 5, @o1.idx, 'Index/key replacement assign failed'
322
+ assert_nil @o2.idx, 'Index/key replacement assign failed'
323
+ assert_nil @o2.key, 'Index/key replacement assign failed'
324
+
325
+ # Replace object by index (new object has different key)
326
+ @a[5] = @o16
327
+ assert_nil @a['new_key'], 'Keyed value replacement failed'
328
+ assert_equal @o16, @a[5], 'Keyed value replacement failed'
329
+ assert_nil @a.hashmap['new_key'], 'Keyed value replacement failed'
330
+ assert_nil @o1.idx, 'Keyed value replacement failed'
331
+ assert_nil @o1.key, 'Keyed value replacement failed'
332
+ assert_equal :key16, @o16.key, 'Keyed value replacement failed' #old key value remains
333
+
334
+ # Replace object by index (new object has same key)
335
+ @a[5] = @o14
336
+ assert_equal :double_key, @o14.key, 'Keyed value replacement failed'
337
+ assert_nil @o13.key, 'Keyed value replacement failed' #old key value is deleted
338
+
339
+ # Assign new key to element already in HashArray
340
+ @a['double_key'] = @o2
341
+ assert_equal @o2, @a['double_key'], 'Key reuse failed'
342
+ assert_equal @o2, @a[2], 'Key reuse failed'
343
+ assert_equal :double_key, @o2.key, 'Key reuse failed'
344
+ assert_nil @a[5], 'Key reuse failed'
345
+
346
+ # Assign to element beyond HashArray border
347
+ @a[25,'out_key'] = @o15
348
+ assert_equal @o15, @a['out_key'], 'Out-of-range index/key assign failed'
349
+ assert_equal @o15, @a[25], 'Out-of-range index/key assign failed'
350
+ assert_equal 25, @o15.idx, 'Index/key replacement assign failed'
351
+ assert_equal :out_key, @o15.key, 'Index/key replacement assign failed'
352
+ end
353
+
354
+ def test_double_keys
355
+ # Add new key to existing element
356
+ @a[:new_key] = @o7
357
+ assert_equal @o7, @a[7], 'Adding second key to element failed'
358
+ assert_equal @o7, @a[:key7], 'Adding second key to element failed'
359
+ assert_equal @o7, @a['new_key'], 'Adding second key to element failed'
360
+
361
+ # Add existing key to new element
362
+ @a[:key2] = @o10
363
+ assert_equal @o10, @a[:key2], 'Adding second key to element failed'
364
+ assert_nil @a[2], 'Adding second key to element failed'
365
+ assert_nil @o2.key, 'Adding second key to element failed'
366
+
367
+ # Add existing key to existing element (existing key belongs to element with multiple keys)
368
+ @a[:new_key] = @o10
369
+ assert_equal @o10, @a[:key2], 'Adding second key to element failed'
370
+ assert_equal @o10, @a['new_key'], 'Adding second key to element failed'
371
+ assert_nil @a[7], 'Adding second key to element failed'
372
+ assert_nil @a[:key7], 'Adding second key to element failed'
373
+ assert_nil @o7.key, 'Adding second key to element failed'
374
+
375
+ # Add 3rd key to existing element
376
+ @a[:third_key] = @o10
377
+ assert_equal @o10, @a[:key2], 'Adding second key to element failed'
378
+ assert_equal @o10, @a[:third_key], 'Adding second key to element failed'
379
+ assert_equal @o10, @a['new_key'], 'Adding second key to element failed'
380
+ end
381
+
382
+ end
383
+
384
+ class A_ReportHATest < Test::Unit::TestCase
385
+
386
+ def setup
387
+ require 'report.rb'
388
+ rep = Report.new 'rep/ArVit187.rep'
389
+ @data = ActiveRecord::Base.establish_dataset(rep)
390
+ @data.owner='ArVitallian'
391
+
392
+ @av = Race.new %w[ArVitallian 13.00 11.61 11.08 5.48 2276614.82 2028341.13 2515 - 2276.61],{}
393
+ @vd = Race.new %w[Vildok 10.33 1.00 1.00 6.07 2035.74 0.00 1 War 0.00], {}
394
+ @raz = Product.new %w[raz 1.00 0 0.00 0.00 0.00 1.00],{:race=>@vd}
395
+ @q = Product.new %w[QAK 1.00 0 0.00 2.33 0.00 3.33],{:race=>@av}
396
+ @s1 = Product.new %w[MoveTo_6300_12504 6299.55 12503.99 0 0],{:race=>@av}
397
+ @mark = Product.new %w[MAPKEP 8.00 0 0.00 0.00 1.00 9.00],{:race=>@av}
398
+ @p1 = Planet.new %w[9802 10483.56 10512.20 N-A4 2500.00 2349.00 2200.00 0.03 QAK 49.75 5214.30 2005.14 2300.00], {:race=>@av, :product=>@q}
399
+ @p2 = Planet.new %w[7346 13906.25 17458.86 CYB 2500.00 0.07 0.00 3819.89], {}
400
+ @p3 = Planet.new %w[7261 15768.34 11160.36], {}
401
+ @f1 = Fleet.new %w[1 fgtr5 6 N-A4 - 0.00 112.50 In_Orbit], {:owner=>'ArVitallian'}
402
+ @g1 = Group.new %w[0 1 MAPKEP 2.22 0.00 0.00 1.00 COL 0.03 N-A4 - 0.00 39.34 9.03 - In_Orbit],
403
+ {:planet=>@p1, :race=>@av, :product=>@mark}
404
+ @g2 = Group.new %w[6830 1170 QAK 13.00 0.00 9.99 0.00 - 0.00 N-A4 - 0.00 78.08 3.33 fgtr5 In_Orbit],
405
+ {:planet=>@p1, :race=>@av, :product=>@q, :fleet=>@f1}
406
+ @races = HashArray.new
407
+ end
408
+
409
+ def test_ha_on_races
410
+ @races << @av
411
+ @races << @vd
412
+ assert_equal @av, @races.first
413
+ assert_equal @av, @races['ArVitallian']
414
+ assert_equal @vd, @races[1]
415
+ assert_equal @vd, @races['Vildok']
416
+ @races << @vd # Adding Vildok race AGAIN... should end up in the same position
417
+ assert_equal @vd, @races[1]
418
+ assert_equal @vd, @races['Vildok']
419
+ assert_equal 2, @races.size
420
+ end
421
+ end