rubylabs 0.6.2 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rubylabs.rb CHANGED
@@ -10,9 +10,8 @@ Methods used to monitor execution of programs during experiments.
10
10
 
11
11
  SCRIPT_LINES__ = Hash.new unless defined? SCRIPT_LINES__
12
12
 
13
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'bin'))
13
+ # $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'bin'))
14
14
 
15
- autoload :Temps, "temps.rb"
16
15
  autoload :SieveLab, "sievelab.rb"
17
16
  autoload :IterationLab, "iterationlab.rb"
18
17
  autoload :RecursionLab, "recursionlab.rb"
@@ -21,7 +20,7 @@ autoload :BitLab, "bitlab.rb"
21
20
  autoload :MARSLab, "marslab.rb"
22
21
  autoload :RandomLab, "randomlab.rb"
23
22
  autoload :EncryptionLab, "encryptionlab.rb"
24
- autoload :ELIZALab, "elizalab.rb"
23
+ autoload :ElizaLab, "elizalab.rb"
25
24
  autoload :SphereLab, "spherelab.rb"
26
25
  autoload :TSPLab, "tsplab.rb"
27
26
 
@@ -479,9 +478,46 @@ Similar to TestArray, but draws random words from a file.
479
478
 
480
479
  end # Source
481
480
 
482
- end # RubyLabs
481
+ =begin rdoc
482
+ Priority queue class -- simple wrapper for an array that can only be updated via
483
+ +<<+ and +shift+ operations. Also responds to +length+, +first+, and +last+,
484
+ and allows direct access to an item through an index expression, but does not allow
485
+ assignment via an index or any other array operation.
486
+ The +<<+ method checks to make sure an object is comparable (responds to <) before
487
+ adding it to the queue.
488
+ =end
483
489
 
484
- include RubyLabs
490
+ class PriorityQueue
491
+
492
+ def initialize
493
+ @q = Array.new
494
+ end
495
+
496
+ def <<(obj)
497
+ raise "Object cannot be inserted into priority queue" unless obj.respond_to?(:<)
498
+ i = 0
499
+ while (i < @q.length)
500
+ break if obj < @q[i]
501
+ i += 1
502
+ end
503
+ @q.insert(i, obj)
504
+ end
505
+
506
+ %w{shift length first last to_s inspect clear empty?}.each do |name|
507
+ eval "def #{name}() @q.#{name} end"
508
+ end
509
+
510
+ def [](i)
511
+ @q[i]
512
+ end
513
+
514
+ def collect(&f)
515
+ @q.map { |x| f.call(x) }
516
+ end
517
+
518
+ end # PriorityQueue
519
+
520
+ end # RubyLabs
485
521
 
486
522
  class Fixnum
487
523
 
@@ -506,3 +542,5 @@ characters (1-letter strings) +ord+ will return the ASCII value.
506
542
  end
507
543
 
508
544
  end # Fixnum
545
+
546
+ include RubyLabs
data/test/bit_test.rb CHANGED
@@ -5,11 +5,13 @@ include BitLab
5
5
 
6
6
  class TestBits < Test::Unit::TestCase
7
7
 
8
+ def test_00_banner
9
+ print "\nBitLab"
10
+ end
11
+
8
12
  # Make some Code objects
9
13
 
10
14
  def test_01_codes
11
- print "\n codes"
12
-
13
15
  c0 = 10.code
14
16
  assert_equal c0.to_s, "1010"
15
17
  assert_equal c0.length, 4
@@ -37,9 +39,7 @@ class TestBits < Test::Unit::TestCase
37
39
 
38
40
  # Parity bit tests
39
41
 
40
- def test_02_parity
41
- print "\n parity"
42
-
42
+ def test_02_parity
43
43
  a = ?A.code(8)
44
44
  assert_equal a.chr, "A"
45
45
  assert a.even_parity?
@@ -65,9 +65,7 @@ class TestBits < Test::Unit::TestCase
65
65
 
66
66
  # Make an encoding scheme for 5 items -- we should get 5 binary codes from 000 to 100
67
67
 
68
- def test_03_make_codes
69
- print "\n make_codes"
70
-
68
+ def test_03_make_codes
71
69
  a = RandomArray.new(:cars, 5)
72
70
  assert_equal a.length, 5
73
71
 
@@ -82,9 +80,7 @@ class TestBits < Test::Unit::TestCase
82
80
 
83
81
  # Make some Message objects
84
82
 
85
- def test_04_messages
86
- print "\n messages"
87
-
83
+ def test_04_messages
88
84
  s = "hello"
89
85
 
90
86
  msg1 = encode(s, :ascii)
@@ -110,8 +106,6 @@ class TestBits < Test::Unit::TestCase
110
106
  # Tree nodes
111
107
 
112
108
  def test_05_nodes
113
- print "\n nodes"
114
-
115
109
  n0 = Node.new("A", 0.2)
116
110
  assert_equal n0.char, "A"
117
111
  assert_equal n0.freq, 0.2
@@ -131,8 +125,6 @@ class TestBits < Test::Unit::TestCase
131
125
  # Priority queue
132
126
 
133
127
  def test_06_priority_queue
134
- print "\n priority queue"
135
-
136
128
  n0 = Node.new("A", 0.2)
137
129
  n1 = Node.new("B", 0.4)
138
130
  n2 = Node.new("C", 0.3)
@@ -154,8 +146,6 @@ class TestBits < Test::Unit::TestCase
154
146
  # Huffman tree and encoding
155
147
 
156
148
  def test_07_huffman
157
- print "\n Huffman tree"
158
-
159
149
  data = File.join(File.dirname(__FILE__), '..', 'data')
160
150
  freq = read_frequencies("#{data}/hafreq.txt")
161
151
 
@@ -0,0 +1,159 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'test_helper'
3
+
4
+ =begin rdoc
5
+ Tests for the Eliza module (the RubyLabs version of Joseph Weizenbaum's ELIZA
6
+ program).
7
+
8
+ The tests make and exercise Pattern and Rule objects used to transform English
9
+ sentences. Tests should try expressions that will be typed interactively by
10
+ students exploring ways of defining rules, and the calls made when rules are
11
+ read from a file.
12
+ =end
13
+
14
+ include ElizaLab
15
+
16
+ class TestEliza < Test::Unit::TestCase
17
+
18
+ def test_00_banner
19
+ print "\nElizaLab"
20
+ end
21
+
22
+ # A Pattern object represents a sentence pattern, which associates a regular
23
+ # expression with a set of possible responses.
24
+
25
+ # Example from MARS tests, showing how to build a "source file" and assemble it:
26
+
27
+ # def test_xx
28
+ # source = [
29
+ # "@label mov #0, #1 ; label needs to be alphanumeric",
30
+ # "l@bel mov #0, #1 ; label needs to be alphanumeric",
31
+ # "mov #0, #1 ; unlabeled line must start with space",
32
+ # "label @bogus #0, #1 ; opcode with non-alphanumeric",
33
+ # "label b@gus #0, #1 ; opcode with non-alphanumeric",
34
+ # "label muv #0, #1 ; unknown opcode",
35
+ # " mov #b@gus, #1 ; messed up operand",
36
+ # " mov #0 #1 ; missing comma",
37
+ # " EQU 3 ; missing label on pseudo-op",
38
+ # "x EQU 3x ; arg not an integer",
39
+ # " END foo ; undefined label",
40
+ # ]
41
+ #
42
+ # name, code, symbols, errors = MARS.assemble(source)
43
+ #
44
+ # assert_equal source.length, errors.length # every line generates an error
45
+ # assert_equal 0, code.length # should be no instructions generated
46
+ # end
47
+
48
+ # The simplest pattern has a literal word, and cycles through sentences when applied
49
+ # to inputs that contain that word
50
+
51
+ def test_01_literal
52
+ p = Pattern.new("father", ["yes", "no"])
53
+ assert p.match("father knows best")
54
+ assert_equal "yes", p.apply("father knows best")
55
+ assert_equal "no", p.apply("father knows best")
56
+ assert_equal "yes", p.apply("father knows best")
57
+ assert_nil p.apply("who's your daddy")
58
+ end
59
+
60
+ # Literals in a pattern are bracketed by \b anchors
61
+
62
+ def test_02_anchors
63
+ p = Pattern.new("father", ["yes"])
64
+ assert_nil p.apply("he is a grandfather")
65
+ assert_equal "yes", p.apply("my father-in-law")
66
+ assert_nil p.apply("land of my fathers")
67
+ end
68
+
69
+ # Test the add_response method
70
+
71
+ def test_03_add_response
72
+ p = Pattern.new("father")
73
+ p.add_response("one")
74
+ p.add_response("two")
75
+ assert_equal "one", p.apply("father knows best")
76
+ assert_equal "two", p.apply("my father-in-law")
77
+ end
78
+
79
+ # Test word groups
80
+
81
+ def test_04_groups
82
+ p = Pattern.new("father|mother")
83
+ assert p.match("father knows best")
84
+ assert p.match("my mother the car")
85
+ end
86
+
87
+ # Match parts used in responses
88
+
89
+ def test_05_match_parts
90
+ p = Pattern.new("I (like|love|adore) my (dog|cat|ducks)")
91
+ p.add_response("You $1 your $2?")
92
+ assert_equal "You like your cat?", p.apply("I like my cat")
93
+ assert_equal "You love your ducks?", p.apply("I love my ducks")
94
+ end
95
+
96
+ # Wild card pattern
97
+
98
+ def test_06_wildcard
99
+ p = Pattern.new("I like (.*)")
100
+ p.add_response("Why do you like $1?")
101
+ assert_equal "Why do you like to drink beer?", p.apply("I like to drink beer")
102
+ assert_equal "Why do you like that car?", p.apply("I like that car")
103
+ end
104
+
105
+ # Postprocessing
106
+
107
+ def test_07_postprocess
108
+ p = Pattern.new("I am (.*)")
109
+ p.add_response("Are you really $1?")
110
+ assert_equal "Are you really afraid of your dog?", p.apply("I am afraid of your dog")
111
+ Eliza.post["I"] = "you"
112
+ Eliza.post["your"] = "my"
113
+ assert_equal "Are you really afraid of my dog?", p.apply("I am afraid of your dog")
114
+ assert_equal "Are you really sorry you dropped my computer?", p.apply("I am sorry I dropped your computer")
115
+ end
116
+
117
+ # Preprocessing
118
+
119
+ def test_08_preprocess
120
+ p = Pattern.new("I am (.*)")
121
+ p.add_response("Are you really $1?")
122
+ assert_nil p.apply("I'm sorry")
123
+ Eliza.pre["I'm"] = "I am"
124
+ assert_equal "Are you really sorry?", p.apply("I'm sorry")
125
+ end
126
+
127
+ # Case insensitive matches
128
+
129
+ def test_09_case
130
+ p = Pattern.new("I am (.*)")
131
+ p.add_response("Are you really $1?")
132
+ assert_equal "Are you really sorry?", p.apply("i am sorry")
133
+ Eliza.pre["I'm"] = "I am"
134
+ assert_equal "Are you really sorry?", p.apply("i'm sorry")
135
+ end
136
+
137
+ # Load the 'doctor' script, try a few sentences -- this test is highly dependent on the rules
138
+ # in the script....
139
+
140
+ # def test_10_doctor
141
+ # assert Eliza.load(:doctor)
142
+ # assert_equal "How do you do? Please state your problem.", Eliza.transform("hello")
143
+ # assert_equal "Do computers worry you?", Eliza.transform("I don't trust computers")
144
+ # assert_equal "Why are you concerned over my hat?", Eliza.transform("I like your hat")
145
+ # assert_equal "How do you know you can't do it?", Eliza.transform("I can't do it")
146
+ # end
147
+
148
+
149
+ # Examples of lines that should be flagged as errors:
150
+
151
+ # unknown keyword:
152
+ #:foo bar
153
+
154
+ # Missing arguments, first arg not a var name:
155
+ #:alias
156
+ #:alias foo bar baz
157
+
158
+ end
159
+
@@ -5,10 +5,13 @@ include EncryptionLab
5
5
 
6
6
  class TestEncryption < Test::Unit::TestCase
7
7
 
8
+ def test_00_banner
9
+ print "\nEncryptionLab"
10
+ end
11
+
8
12
  # Caesar cypher
9
13
 
10
14
  def test_01_caesar
11
- print "\n Caesar cypher"
12
15
  assert_equal caesar("abcdefghijklmnopqrstuvwxyz"), "defghijklmnopqrstuvwxyzabc"
13
16
  assert_equal caesar("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), "DEFGHIJKLMNOPQRSTUVWXYZABC"
14
17
  assert_equal caesar("abcdefghijklmnopqrstuvwxyz", 13), "nopqrstuvwxyzabcdefghijklm"
@@ -5,10 +5,13 @@ include IterationLab
5
5
 
6
6
  class TestIterativeAlgoritms < Test::Unit::TestCase
7
7
 
8
+ def test_00_banner
9
+ print "\nIterationLab"
10
+ end
11
+
8
12
  # Test the contains? method (our version of include?)
9
13
 
10
14
  def test_01_contains
11
- print "\n contains?"
12
15
  a = ["fee", "fie", "foe", "fum"]
13
16
  assert contains?(a, "fee")
14
17
  assert contains?(a, "foe")
@@ -19,7 +22,6 @@ class TestIterativeAlgoritms < Test::Unit::TestCase
19
22
  # Same as above, but using the location method (our version of index)
20
23
 
21
24
  def test_02_search
22
- print "\n search"
23
25
  a = ["fee", "fie", "foe", "fum"]
24
26
  assert_equal 0, search(a, "fee")
25
27
  assert_equal 2, search(a, "foe")
@@ -30,7 +32,6 @@ class TestIterativeAlgoritms < Test::Unit::TestCase
30
32
  # Make some test arrays, sort them
31
33
 
32
34
  def test_02_msort
33
- print "\n msort"
34
35
  [15, 16, 17].each do |size|
35
36
  a = TestArray.new(size)
36
37
  assert_equal isort(a), a.sort
data/test/mars_test.rb CHANGED
@@ -33,6 +33,10 @@ include MARSLab
33
33
 
34
34
  class TestMARS < Test::Unit::TestCase
35
35
 
36
+ def test_00_banner
37
+ print "\nMARSLab"
38
+ end
39
+
36
40
  # Assembling these instructions should lead to one error per line, and
37
41
  # no code generated.
38
42
 
data/test/random_test.rb CHANGED
@@ -5,6 +5,10 @@ include RandomLab
5
5
 
6
6
  class TestRandoms < Test::Unit::TestCase
7
7
 
8
+ def test_00_banner
9
+ print "\nRandomLab"
10
+ end
11
+
8
12
  # The prng_sequence method makes a sequence of numbers using a linear congruential
9
13
  # method with the specified a, c, and m values
10
14
 
@@ -5,12 +5,15 @@ include RecursionLab
5
5
 
6
6
  class TestRecursiveAlgoritms < Test::Unit::TestCase
7
7
 
8
+ def test_00_banner
9
+ print "\nRecursionLab"
10
+ end
11
+
8
12
  # Make some test arrays, try successful and unsuccessful searches.
9
13
  # Test array sizes students will use (one less than power of two)
10
14
  # plus some odd ones.
11
15
 
12
16
  def test_01_bsearch
13
- print "\n bsearch"
14
17
  [14, 15, 16].each do |size|
15
18
  a = TestArray.new(size).sort
16
19
  assert_not_nil bsearch(a, a.random(:success))
@@ -23,7 +26,6 @@ class TestRecursiveAlgoritms < Test::Unit::TestCase
23
26
  # the array is not modified
24
27
 
25
28
  def test_02_msort
26
- print "\n msort"
27
29
  [15, 16, 17].each do |size|
28
30
  a = TestArray.new(size)
29
31
  b = a.dup
@@ -35,7 +37,6 @@ class TestRecursiveAlgoritms < Test::Unit::TestCase
35
37
  # Array size not a factor here -- just try a few sorts
36
38
 
37
39
  def test_03_qsort
38
- print "\n qsort"
39
40
  [15, 16, 17].each do |size|
40
41
  a = TestArray.new(size)
41
42
  b = a.dup
@@ -3,16 +3,15 @@ require 'test_helper'
3
3
 
4
4
  class RubyLabsTest < Test::Unit::TestCase
5
5
 
6
- def test_00_arrays
7
- print "\n test_array"
6
+ def test_00_banner
7
+ print "\nRubyLabs"
8
+ end
9
+
10
+ def test_01_arrays
8
11
  a = TestArray.new(10)
9
12
  assert a.length == 10
10
13
  assert a.include?(a.random(:success))
11
14
  assert !a.include?(a.random(:fail))
12
15
  end
13
16
 
14
- def test_01_scope
15
- print "\n RubyScope [TBD]"
16
- end
17
-
18
17
  end
data/test/sieve_test.rb CHANGED
@@ -5,11 +5,14 @@ include SieveLab
5
5
 
6
6
  class TestSieve < Test::Unit::TestCase
7
7
 
8
+ def test_00_banner
9
+ print "\nSieveLab"
10
+ end
11
+
8
12
  # Compare list generated by sieve method with list from Wikipedia. Test both the main
9
13
  # version (sieve) and the simple prototype that iterates until the worklist is empty.
10
14
 
11
15
  def test_01_first_25
12
- print "\n sieve"
13
16
  expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
14
17
  assert_equal expected, sieve(100)
15
18
  assert_equal expected, proto_sieve(100)
@@ -19,10 +22,10 @@ class TestSieve < Test::Unit::TestCase
19
22
  # pi[i] is the number of primes less than 10**i. Note: calling sieve(n) with n less than 2
20
23
  # should quietly return an empty list.
21
24
 
22
- def test_02_pi
23
- pi = [0, 4, 25, 168, 1229, 9592]
24
- for i in 0..(pi.length-1)
25
- assert_equal pi[i], sieve(10**i).length
26
- end
27
- end
25
+ def test_02_pi
26
+ pi = [0, 4, 25, 168, 1229] # 9592
27
+ for i in 0..(pi.length-1)
28
+ assert_equal pi[i], sieve(10**i).length
29
+ end
30
+ end
28
31
  end
data/test/sphere_test.rb CHANGED
@@ -18,11 +18,13 @@ include SphereLab
18
18
 
19
19
  class TestSpheres < Test::Unit::TestCase
20
20
 
21
+ def test_00_banner
22
+ print "\nSphereLab"
23
+ end
24
+
21
25
  # Vector arithmetic. Note: calls to assert_equal will also test the == method.
22
26
 
23
27
  def test_01_vectors
24
- print "\n vectors"
25
-
26
28
  v1 = Vector.new(1, 1, 1)
27
29
  v2 = Vector.new(2, 2, 2)
28
30
  v3 = Vector.new(3, 3, 3)
@@ -44,7 +46,6 @@ class TestSpheres < Test::Unit::TestCase
44
46
  # each other on the y axis).
45
47
 
46
48
  def test_02_bodies
47
- print "\n bodies"
48
49
  y1 = 1000
49
50
  y2 = 0
50
51
  b1 = Body.new(1000000, Vector.new(0, y1, 0), Vector.new(0, 0, 0))
@@ -64,7 +65,6 @@ class TestSpheres < Test::Unit::TestCase
64
65
  # uses the same force twice)
65
66
 
66
67
  def test_03_interaction
67
- print "\n interaction"
68
68
  y1 = 1000
69
69
  y2 = 0
70
70
  b1 = Body.new(1000000, Vector.new(0, y1, 0), Vector.new(0, 0, 0))
@@ -85,7 +85,6 @@ class TestSpheres < Test::Unit::TestCase
85
85
  # close to where it started, and earth to be about 1/4 through its orbit.
86
86
 
87
87
  def test_04_solar_system
88
- print "\n solar system"
89
88
 
90
89
  bodies = [
91
90
  Body.new(1.989E30, Vector.new(0,0.000E00,0), Vector.new(0.000E0,0,0), "sun"),
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubylabs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - conery
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-11 00:00:00 -08:00
12
+ date: 2010-02-08 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -44,10 +44,24 @@ files:
44
44
  - data/century.txt
45
45
  - data/colors.txt
46
46
  - data/earth.yaml
47
+ - data/eliza/doctor.txt
47
48
  - data/fruit.txt
48
49
  - data/hacodes.txt
49
50
  - data/hafreq.txt
50
51
  - data/hvfreq.txt
52
+ - data/mars/chang1.txt
53
+ - data/mars/dwarf.txt
54
+ - data/mars/ferret.txt
55
+ - data/mars/imp.txt
56
+ - data/mars/mice.txt
57
+ - data/mars/midget.txt
58
+ - data/mars/piper.txt
59
+ - data/mars/plague.txt
60
+ - data/mars/test_celsius.txt
61
+ - data/mars/test_div.txt
62
+ - data/mars/test_hello.txt
63
+ - data/mars/test_mult.txt
64
+ - data/mars/test_threads.txt
51
65
  - data/nbody.R
52
66
  - data/nbody.out
53
67
  - data/nbody.pdf
@@ -103,6 +117,7 @@ specification_version: 3
103
117
  summary: Software and data for lab projects in the Science of Computing text.
104
118
  test_files:
105
119
  - test/bit_test.rb
120
+ - test/eliza_test.rb
106
121
  - test/encryption_test.rb
107
122
  - test/iteration_test.rb
108
123
  - test/mars_test.rb