Skip to content

Commit

Permalink
playlist & it's spec file. should be thread safe...
Browse files Browse the repository at this point in the history
  • Loading branch information
Dayne Broderson committed May 23, 2008
1 parent 436ac07 commit e47ab07
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
63 changes: 63 additions & 0 deletions lib/playlist.rb
@@ -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
2 changes: 1 addition & 1 deletion shoutr
Expand Up @@ -38,7 +38,7 @@ ARGV.each do |filename|
next unless target
if File.extname(target).downcase == '.mp3'
puts "got an mp3 file, trying new code"
cmd = ("mpg123 -q -w - '#{target}' | oggenc -c '#{target}' -Q -")
cmd = ("mpg123 -q -w - \"#{target}\" | oggenc -c \"#{target}\" -Q -")
IO.popen(cmd,'r') { |p| send_io(s,p,filename) }
elsif File.extname(target).downcase == '.ogg'
puts "got an ogg file, using old code"
Expand Down
59 changes: 59 additions & 0 deletions spec/playlist_spec.rb
@@ -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

0 comments on commit e47ab07

Please sign in to comment.