square_graph 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/square_graph.rb CHANGED
@@ -5,10 +5,10 @@ class SquareGraph
5
5
  attr_reader :length, :width
6
6
 
7
7
  def initialize(*dimen)
8
- raise ArgumentError, "Only zero or two parameters" if ![0,2].include?(dimen.size)
8
+ raise ArgumentError if ![0,2].include?(dimen.size)
9
9
  if dimen.size == 2
10
- raise ArgumentError if dimen[0].class != Fixnum or dimen[1].class != Fixnum
11
- raise ArgumentError if dimen[0] < 1 or dimen[1] < 1
10
+ test_fixnum(dimen[0], dimen[1])
11
+ raise ArgumentError if test_range([1, dimen[0]], [1, dimen[1]])
12
12
  @length, @width = dimen[0], dimen[1]
13
13
  @sized = true
14
14
  else
@@ -19,23 +19,51 @@ class SquareGraph
19
19
 
20
20
  def resize(length, width)
21
21
  raise NoMethodError if @sized == false
22
- raise ArgumentError if length.class != Fixnum or width.class != Fixnum
23
- raise ArgumentError if length < 1 or width < 1
22
+ test_fixnum(length, width)
23
+ return nil if test_range([1, length], [1, width])
24
24
  @length, @width = length, width
25
25
  end
26
26
 
27
27
  def fill(*args)
28
28
  x, y = args[0], args[1]
29
+ test_fixnum(x, y)
30
+ if @sized == true
31
+ return nil if test_range([1, x], [1, y], [x, @length], [y, @length])
32
+ end
29
33
  @sg[[x,y]] = Face.new(x, y, true)
30
34
  end
31
35
 
32
36
  def get(x, y)
37
+ return nil if !@sg[[x,y]]
33
38
  @sg[[x,y]].object
34
39
  end
35
40
 
36
- def insert
41
+ def remove(*args)
42
+ x, y = args[0], args[1]
43
+ test_fixnum(x, y)
44
+ @sg[[x, y]] = nil
45
+ end
46
+
47
+ def clear
48
+ @sg.clear
37
49
  end
38
50
 
39
- def get_truthy(x, y)
51
+ def empty?
52
+ !(@sg.any? do |f|
53
+ @sg[f[0]] and @sg[f[0]].object
54
+ end)
40
55
  end
56
+
57
+ private
58
+
59
+ def test_fixnum(*value)
60
+ value.each do |v|
61
+ raise ArgumentError if v.class != Fixnum
62
+ end
63
+ end
64
+
65
+ def test_range(*value)
66
+ value.any? {|pair| pair[0] > pair[1]}
67
+ end
68
+
41
69
  end
@@ -5,7 +5,7 @@ describe SquareGraph, '#new' do
5
5
  SquareGraph.new(5,5).should_not be_nil
6
6
  end
7
7
  it "should error when given one parameter" do
8
- expect {SquareGraph.new(0)}.to raise_error(ArgumentError, "Only zero or two parameters")
8
+ expect {SquareGraph.new(0)}.to raise_error(ArgumentError)
9
9
  end
10
10
  it "should accept no parameters" do
11
11
  SquareGraph.new.should_not be_nil
@@ -19,7 +19,7 @@ describe SquareGraph, '#new' do
19
19
  expect {SquareGraph.new("lalaland", "luftland")}.to raise_error(ArgumentError)
20
20
  end
21
21
  it "should only save positive integers into the parameters" do
22
- expect {SquareGraph.new(0,5)}.to raise_error(ArgumentError)
22
+ expect {SquareGraph.new(0, 5)}.to raise_error(ArgumentError)
23
23
  expect {SquareGraph.new(5,-1)}.to raise_error(ArgumentError)
24
24
  end
25
25
  it "should not save len/wid if no parameters are given" do
@@ -42,24 +42,103 @@ describe SquareGraph, "#resize" do
42
42
  end
43
43
  it "should only resize to valid sizes" do
44
44
  sg = SquareGraph.new(5,5)
45
- expect {sg.resize(0, -1)}.to raise_error(ArgumentError)
45
+ sg.resize(0, -1).should be_nil
46
46
  end
47
47
  it "should not be able to resize if conflicts" do
48
48
  #TODO yeah, let's implement this later
49
49
  end
50
50
  end
51
51
 
52
- describe SquareGraph, "#fill" do
53
- it "simply fills in a location with a simple TrueClass" do
52
+ describe SquareGraph, "#fill, #get" do
53
+ it "fills a single position with a TrueClass and gets it back with get" do
54
54
  sg = SquareGraph.new(5,5)
55
55
  sg.fill(2,2)
56
56
  sg.get(2,2).should eql(true)
57
57
  end
58
+ it "doesn't allow fills of outside the graph" do
59
+ sg = SquareGraph.new(5,10)
60
+ sg.fill(10, 2).should be_nil
61
+ sg.fill(2, 11).should be_nil
62
+ sg.fill(0, 0).should be_nil
63
+ #TODO there should be a better way to implement these.. srsly
64
+ end
65
+ it "doesn't allow random values" do
66
+ sg = SquareGraph.new(2,2)
67
+ expect {sg.fill("blah", 10)}.to raise_error(ArgumentError)
68
+ expect {sg.fill(0.1, 2)}.to raise_error(ArgumentError)
69
+ end
70
+ it "allows undimentioned graphs to fill anywhere, even negative" do
71
+ sg = SquareGraph.new
72
+ sg.fill(2,2)
73
+ sg.fill(-1, -3)
74
+ sg.get(-1, -3).should eql(true)
75
+ end
76
+ it "nils if getting something that doesn't exist yet" do
77
+ sg = SquareGraph.new
78
+ sg.get(0,0).should be_nil
79
+ end
80
+ it "nils when getting something out of the range of the graph" do
81
+ sg = SquareGraph.new(5,5)
82
+ sg.get(2,2).should be_nil
83
+ sg.get(10,10).should be_nil
84
+ end
58
85
  end
59
86
 
60
87
  describe SquareGraph, "#insert" do
61
88
  it "fills a location with an object" do
62
- #TODO this one gets rly hard.
89
+ #TODO this one gets rly hard. don't know if we should put it into fill
63
90
  end
64
91
  end
65
92
 
93
+ describe SquareGraph, "#remove" do
94
+ it "removes any objects from a certain position" do
95
+ sg = SquareGraph.new
96
+ sg.fill(2,2)
97
+ sg.get(2,2).should eql(true)
98
+ sg.remove(2,2)
99
+ sg.get(2,2).should be_nil
100
+ end
101
+ it "returns nil if there was nothing in the position" do
102
+ sg = SquareGraph.new
103
+ sg.remove(2, 2).should be_nil
104
+ end
105
+ it "returns nil if you try and remove something outside of rannge" do
106
+ sg = SquareGraph.new(5, 5)
107
+ sg.remove(10, 10).should be_nil
108
+ end
109
+ end
110
+
111
+ describe SquareGraph, "#clear" do
112
+ it "removes every object in the graph" do
113
+ sg = SquareGraph.new
114
+ (0..5).each {|x| sg.fill(x, x)}
115
+ (1..6).each {|x| sg.get(x-1, x-1).should eql(true)}
116
+ sg.clear
117
+ (2..7).each {|x| sg.get(x-2, x-2).should be_nil}
118
+ end
119
+ end
120
+
121
+ describe SquareGraph, "#empty?" do
122
+ it "informs us if SquareGraph is empty, ie, if none of the positions are filled" do
123
+ sg = SquareGraph.new(5, 5)
124
+ (1..5).each do |x|
125
+ (1..5).each do |y|
126
+ sg.fill(x,y)
127
+ end
128
+ end
129
+ sg.get(3, 3).should eql(true)
130
+ sg.clear
131
+ sg.empty?.should eql(true)
132
+ end
133
+ it "informs us even if we just remove the items" do
134
+ sg = SquareGraph.new
135
+ sg.fill(0,0)
136
+ sg.remove(0, 0)
137
+ sg.empty?.should eql(true)
138
+ end
139
+ it "returns false if the graph isn't empty" do
140
+ sg = SquareGraph.new
141
+ sg.fill(0,0)
142
+ sg.empty?.should eql(false)
143
+ end
144
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: square_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-23 00:00:00.000000000 Z
12
+ date: 2012-02-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: No description for now.
14
+ description: So far, you can create true's all over a graph that's either dimentioned
15
+ or not.\n Later, the world!
15
16
  email: Austin.L.D4L@gmail.com
16
17
  executables: []
17
18
  extensions: []
@@ -43,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
44
  version: '0'
44
45
  requirements: []
45
46
  rubyforge_project:
46
- rubygems_version: 1.8.15
47
+ rubygems_version: 1.8.17
47
48
  signing_key:
48
49
  specification_version: 3
49
50
  summary: a better 2d array