cellular_map 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,6 +37,10 @@ module CellularMap
37
37
  [@x, @y, @map] == [other.x, other.y, other.map]
38
38
  end
39
39
 
40
+ def <=>(other) # :nodoc:
41
+ 2 * (y <=> other.y) + (x <=> other.x)
42
+ end
43
+
40
44
  def eql?(other) # :nodoc:
41
45
  self == other
42
46
  end
@@ -44,7 +44,7 @@ module CellularMap
44
44
  end
45
45
 
46
46
  def ==(other) # :nodoc:
47
- [@x, @y, @map] == [other.x, other.y, other.map]
47
+ ! self.detect { |c| ! (other.map[c.x, c.y].content == c.content) }
48
48
  end
49
49
 
50
50
  # Iterating over each cells inside the zone.
@@ -79,5 +79,10 @@ module CellularMap
79
79
  def rangeize(x, y)
80
80
  [x, y].collect { |i| i.respond_to?(:to_i) ? (i.to_i..i.to_i) : i }
81
81
  end
82
+
83
+ def initialize_copy(other) # :nodoc:
84
+ @map = other.map.dup
85
+ @map.each { |c| c.content = nil unless x === c.x && y === c.y }
86
+ end
82
87
  end
83
88
  end
@@ -39,25 +39,40 @@ class TestCell < Test::Unit::TestCase
39
39
  context("An array of cells") {
40
40
  setup {
41
41
  @map = CellularMap::Map.new
42
- @cells = [
42
+ @sorted = [
43
+ @map[6, -9],
43
44
  @map[0, 0],
45
+ @map[0, 5],
44
46
  @map[1, 5],
45
47
  @map[-4, 7],
48
+ @map[0, 7],
49
+ @map[3, 7],
50
+ @map[3, 8],
51
+ ]
52
+ @cells = [
46
53
  @map[3, 8],
47
- @map[6, -9]
54
+ @map[3, 7],
55
+ @map[1, 5],
56
+ @map[0, 0],
57
+ @map[0, 7],
58
+ @map[-4, 7],
59
+ @map[0, 5],
60
+ @map[6, -9],
48
61
  ]
49
62
  @duplicated = @cells + [
50
63
  @map[0, 0],
51
64
  @map[1, 5],
52
65
  ]
53
- @sort_order = lambda { |a, b| 2 * (a[0] <=> b[0]) + (a[1] <=> b[1]) }
54
66
  }
55
67
 
68
+ should("be easy to sort (by rows then by columns)") {
69
+ assert_equal @sorted, @cells.sort }
70
+
56
71
  should("remove duplicated elements") {
57
72
  expected, obtained = [
58
73
  @cells,
59
74
  @duplicated.uniq
60
- ].collect { |a| a.collect { |c| [c.x, c.y] }.sort &@sort_order }
75
+ ].collect { |a| a.collect { |c| [c.x, c.y] }.sort }
61
76
  assert_equal expected, obtained
62
77
  }
63
78
  }
@@ -95,6 +95,22 @@ class TestZone < Test::Unit::TestCase
95
95
  } }
96
96
  assert_equal @positions, inside
97
97
  }
98
+
99
+ context("and its duplicate") {
100
+ setup { @duplicate = @zone.dup }
101
+ should("be identical") { assert_equal @zone, @duplicate }
102
+ should("not be the same instance") {
103
+ assert_not_same @zone, @duplicate }
104
+ should("not be on the same map") {
105
+ assert_not_same @zone.map, @duplicate.map }
106
+ should("have duplicated only the cells inside the zone") {
107
+ expected, obtained = [
108
+ @duplicate.map.to_a,
109
+ @duplicate.reject { |c| c.content.nil? }
110
+ ].collect { |a| a.sort }
111
+ assert_equal expected, obtained
112
+ }
113
+ }
98
114
  }
99
115
  }
100
116
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cellular_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Belleville