Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
playlist & it's spec file. should be thread safe...
- Loading branch information
Dayne Broderson
committed
May 23, 2008
1 parent
436ac07
commit e47ab07
Showing
3 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'thread' | ||
####### Queue methods | ||
# << clear deq empty? | ||
# enq length new num_waiting | ||
# pop push shift size | ||
class Queue | ||
attr_reader :que | ||
end | ||
|
||
class Playlist < Queue | ||
|
||
def insert(index,obj) | ||
@que.insert(index,obj) # TODO | ||
end | ||
|
||
def shuffle | ||
@que.shuffle! # TODO | ||
end | ||
|
||
def clear | ||
@que = [] | ||
end | ||
|
||
def move(cur_pos,new_pos) | ||
cur_pos=cur_pos.to_i | ||
new_pos=new_pos.to_i | ||
if (@que.size<= cur_pos) || (@que.size<= new_pos) || (cur_pos<0) || (new_pos<0) | ||
raise "Out of Bounds" | ||
end | ||
insert(new_pos,delete_at(cur_pos)) | ||
end | ||
|
||
def copy(cur_pos,new_pos) | ||
cur_pos=cur_pos.to_i | ||
new_pos=new_pos.to_i | ||
if (@que.size<= cur_pos) || (cur_pos<0) || (new_pos<0) | ||
raise "Out of Bounds" | ||
end | ||
if (@que.size<= new_pos) | ||
enq(@que.at(cur_pos)) | ||
return @que.size | ||
else | ||
insert(new_pos,@que.at(cur_pos)) | ||
return new_pos | ||
end | ||
end | ||
|
||
def delete_at(index) | ||
@que.delete_at(index.to_i) # TODO | ||
end | ||
|
||
def [](index) | ||
@que[index] | ||
end | ||
|
||
def pull(index) | ||
# TODO | ||
end | ||
end | ||
|
||
if __FILE__ == $0 | ||
# perhaps add some testing stuff here | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
$: << File.join( File.dirname( __FILE__ ), '../lib' ) | ||
|
||
require 'rubygems' | ||
require 'spec' | ||
require 'playlist' | ||
require 'pp' | ||
|
||
context "Testing shoutr playlist" do | ||
setup do | ||
@p = Playlist.new | ||
@p.enq "a"; @p.enq "b"; @p.enq "c" | ||
end | ||
|
||
specify "begins test with 3 elements and can be cleared" do | ||
@p.size.should == 3 | ||
@p.clear | ||
@p.size.should == 0 | ||
end | ||
|
||
specify "adding element should increase playlist size" do | ||
@p.enq "d" | ||
@p.size.should == 4 | ||
@p.deq.should == "a" | ||
@p.size.should == 3 | ||
end | ||
|
||
specify "copying an element should move it" do | ||
@p[0].should == "a" | ||
@p[2].should == "c" | ||
lambda {@p.move(2,0)}.should_not raise_error("Out of Bounds") | ||
@p[0].should == "c" | ||
@p[2].should == "b" | ||
end | ||
|
||
specify "copying out of range should cause an error" do | ||
lambda {@p.move(0,4)}.should raise_error("Out of Bounds") | ||
end | ||
|
||
specify "removing an element" do | ||
@p.delete_at(1) | ||
@p[0].should == "a" | ||
@p[1].should == "c" | ||
@p.size.should == 2 | ||
end | ||
|
||
specify "copying an element" do | ||
@p.copy(0,3) # a b c a | ||
@p[3].should == "a" | ||
@p.copy(1,0) # b a b c a | ||
@p[0].should == "b" | ||
lambda {@p.copy(13,0)}.should raise_error("Out of Bounds") | ||
end | ||
|
||
specify "shuffling test" do | ||
@p.shuffle | ||
pp @p | ||
@p[0].should != "a" | ||
end | ||
end |