Created
July 23, 2012 18:19
-
-
Save banyan/3165174 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
describe Array do | |
let(:array) { [1,2,3,4,5] } | |
subject &:array | |
it "先頭の要素(1)を返すこと" do | |
subject.first.should == 1 | |
subject[0].should == 1 | |
end | |
it "末尾の要素(5)を返すこと" do | |
subject[-1].should == 5 | |
subject.last.should == 5 | |
end | |
it "配列のn番目の要素を得ること" do | |
subject.fetch(3).should == 4 | |
subject[3] == 4 | |
subject.at(3) == 4 | |
subject.slice(3) == 4 | |
end | |
it "配列が空かどうかを確認すること" do | |
subject.size.should_not == 0 end | |
it "要素をランダムに一個返すこと" do | |
should include subject.sample | |
end | |
it "全ての要素をランダムに入れ替えること" do | |
subject.shuffle.should =~ [1,2,3,4,5] | |
end | |
it "配列を複製すること" do | |
subject.clone.should == subject | |
subject.dup.should == subject | |
end | |
it "配列を空にすること" do | |
subject.clear.should == [] | |
end | |
context "末尾に要素(6)を追加すること" do | |
it { subject.push(6).should == [1,2,3,4,5,6] } | |
it { (subject << 6).should == [1,2,3,4,5,6] } | |
end | |
it "末尾の要素(5)を削除すること" do | |
subject[0...-1].should == [1,2,3,4] | |
end | |
it "先頭に要素(0)を追加すること" do | |
subject.unshift(0).should == [0,1,2,3,4,5] | |
end | |
it "先頭の要素(1)を削除すること" do | |
subject.slice(1..-1).should == [2,3,4,5] | |
subject.shift | |
should == [2,3,4,5] | |
end | |
it '[1,2,3,4,5] を 文字列の "1-2-3-4-5" にすること' do | |
subject.join("-").should == "1-2-3-4-5" | |
(subject * "-").should == "1-2-3-4-5" | |
(subject*?-).should == "1-2-3-4-5" | |
end | |
it "[1,2,3,4,5] を [1,2,3,'ruby',4,5] にすること" do | |
subject.insert(3, 'ruby').should == [1,2,3,'ruby',4,5] | |
end | |
it "[1,3,2,5,4] を [1,2,3,4,5] にすること" do | |
[1,3,2,5,4].sort.should == [1,2,3,4,5] | |
[1,3,2,5,4].sort{|a,b| a <=> b }.should == [1,2,3,4,5] | |
end | |
it "[1,2,3,4,5] を [5,4,3,2,1] にすること" do | |
[1,2,3,4,5].reverse.should == [5,4,3,2,1] | |
[1,2,3,4,5].sort{|a,b| b <=> a }.should == [5,4,3,2,1] | |
end | |
end | |
# Array | |
# 先頭の要素(1)を返すこと | |
# 末尾の要素(5)を返すこと | |
# 配列のn番目の要素を得ること | |
# 配列が空かどうかを確認すること | |
# 要素をランダムに一個返すこと | |
# 全ての要素をランダムに入れ替えること | |
# 配列を複製すること | |
# 配列を空にすること | |
# 末尾の要素(5)を削除すること | |
# 先頭に要素(0)を追加すること | |
# 先頭の要素(1)を削除すること | |
# [1,2,3,4,5] を 文字列の "1-2-3-4-5" にすること | |
# [1,2,3,4,5] を [1,2,3,'ruby',4,5] にすること | |
# [1,3,2,5,4] を [1,2,3,4,5] にすること | |
# [1,2,3,4,5] を [5,4,3,2,1] にすること | |
# 末尾に要素(6)を追加すること | |
# should == [1, 2, 3, 4, 5, 6] | |
# should == [1, 2, 3, 4, 5, 6] | |
# Finished in 0.00485 seconds | |
# 17 examples, 0 failures |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment