circular_list 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +15 -0
- data/circular_list.gemspec +20 -0
- data/lib/circular_list.rb +21 -0
- data/lib/circular_list/version.rb +3 -0
- data/spec/circular_list/list_spec.rb +84 -0
- data/spec/spec_helper.rb +1 -0
- metadata +76 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'bundler'
|
4
|
+
|
5
|
+
Bundler.setup
|
6
|
+
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
|
9
|
+
desc "Run all examples"
|
10
|
+
Rspec::Core::RakeTask.new(:spec) do |t|
|
11
|
+
t.rspec_opts = %w[--color --format documentation]
|
12
|
+
t.verbose = false
|
13
|
+
end
|
14
|
+
|
15
|
+
task :default => :spec
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "circular_list/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "circular_list"
|
7
|
+
s.version = CircularList::VERSION
|
8
|
+
s.authors = ["unnitallman"]
|
9
|
+
s.email = ["unni.tallman@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A circular list data structure for Ruby}
|
12
|
+
s.description = %q{A circular list data structure for Ruby}
|
13
|
+
|
14
|
+
s.rubyforge_project = "circular_list"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "circular_list/version"
|
2
|
+
|
3
|
+
module CircularList
|
4
|
+
class List
|
5
|
+
def initialize(array)
|
6
|
+
@arr = array
|
7
|
+
end
|
8
|
+
|
9
|
+
def size
|
10
|
+
@arr.size
|
11
|
+
end
|
12
|
+
|
13
|
+
def fetch_previous(index=0)
|
14
|
+
@arr.unshift(@arr.pop)[index]
|
15
|
+
end
|
16
|
+
|
17
|
+
def fetch_next(index=0)
|
18
|
+
@arr.push(@arr.shift)[index]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module CircularList
|
4
|
+
describe List do
|
5
|
+
context "when array is empty" do
|
6
|
+
it "fetch_next should return nil" do
|
7
|
+
List.new([]).fetch_next.should eq nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it "fetch_previous should return nil" do
|
11
|
+
List.new([]).fetch_previous.should eq nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when array is nil" do
|
16
|
+
it "fetch_next should return nil" do
|
17
|
+
List.new([]).fetch_next.should eq nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it "fetch_previous should return nil" do
|
21
|
+
List.new([]).fetch_previous.should eq nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when array is non-empty" do
|
26
|
+
context "and no index specified" do
|
27
|
+
it "fetch_next should return next element" do
|
28
|
+
c = List.new([1,2,3,4])
|
29
|
+
c.fetch_next.should eq 2
|
30
|
+
c.fetch_next.should eq 3
|
31
|
+
c.fetch_next.should eq 4
|
32
|
+
c.fetch_next.should eq 1
|
33
|
+
c.fetch_next.should eq 2
|
34
|
+
end
|
35
|
+
|
36
|
+
it "fetch_previous should return previous element" do
|
37
|
+
c = List.new([1,2,3,4])
|
38
|
+
c.fetch_previous.should eq 4
|
39
|
+
c.fetch_previous.should eq 3
|
40
|
+
c.fetch_previous.should eq 2
|
41
|
+
c.fetch_previous.should eq 1
|
42
|
+
c.fetch_previous.should eq 4
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when a valid index is specified" do
|
47
|
+
it "fetch_next should return next element after index" do
|
48
|
+
c = List.new(['a','b','c','d'])
|
49
|
+
c.fetch_next(2).should eq 'd'
|
50
|
+
|
51
|
+
c = List.new(['a','b','c','d'])
|
52
|
+
c.fetch_previous(0).should eq 'd'
|
53
|
+
end
|
54
|
+
|
55
|
+
it "fetch_previous should return previous element before index" do
|
56
|
+
c = List.new(['a','b','c','d'])
|
57
|
+
c.fetch_previous(2).should eq 'b'
|
58
|
+
|
59
|
+
c = List.new(['a','b','c','d'])
|
60
|
+
c.fetch_previous(0).should eq 'd'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "when an invalid index is specified" do
|
65
|
+
it "fetch_next should return nil" do
|
66
|
+
c = List.new(['a','b','c','d'])
|
67
|
+
c.fetch_next(12).should eq nil
|
68
|
+
|
69
|
+
c = List.new(['a','b','c','d'])
|
70
|
+
c.fetch_previous(12).should eq nil
|
71
|
+
end
|
72
|
+
|
73
|
+
it "fetch_previous should return nil" do
|
74
|
+
c = List.new(['a','b','c','d'])
|
75
|
+
c.fetch_previous(12).should eq nil
|
76
|
+
|
77
|
+
c = List.new(['a','b','c','d'])
|
78
|
+
c.fetch_previous(12).should eq nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'circular_list'
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: circular_list
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- unnitallman
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-09-27 00:00:00 +05:30
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A circular list data structure for Ruby
|
23
|
+
email:
|
24
|
+
- unni.tallman@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- Rakefile
|
35
|
+
- circular_list.gemspec
|
36
|
+
- lib/circular_list.rb
|
37
|
+
- lib/circular_list/version.rb
|
38
|
+
- spec/circular_list/list_spec.rb
|
39
|
+
- spec/spec_helper.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: ""
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: circular_list
|
70
|
+
rubygems_version: 1.4.2
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: A circular list data structure for Ruby
|
74
|
+
test_files:
|
75
|
+
- spec/circular_list/list_spec.rb
|
76
|
+
- spec/spec_helper.rb
|