rubylabs 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +9 -0
  3. data/Rakefile +63 -0
  4. data/VERSION +1 -0
  5. data/bin/bb.rb +12 -0
  6. data/bin/statistics2-0.53/ext/extconf.rb +11 -0
  7. data/bin/statistics2-0.53/ext/show.rb +11 -0
  8. data/bin/statistics2-0.53/ext/t.rb +46 -0
  9. data/bin/statistics2-0.53/mklist.rb +26 -0
  10. data/bin/statistics2-0.53/sample-tbl.rb +129 -0
  11. data/bin/statistics2-0.53/statistics2.rb +532 -0
  12. data/bin/statistics2-0.53/t-inv.rb +54 -0
  13. data/bin/statistics2.rb +532 -0
  14. data/data/aafreq.txt +20 -0
  15. data/data/cars.txt +50 -0
  16. data/data/century.txt +1 -0
  17. data/data/colors.txt +64 -0
  18. data/data/earth.yaml +15 -0
  19. data/data/fruit.txt +45 -0
  20. data/data/hacodes.txt +35 -0
  21. data/data/hafreq.txt +16 -0
  22. data/data/hvfreq.txt +5 -0
  23. data/data/nbody.R +23 -0
  24. data/data/nbody.out +731 -0
  25. data/data/nbody.pdf +3111 -0
  26. data/data/nbody.png +0 -0
  27. data/data/nbody3d.pdf +3201 -0
  28. data/data/outer.pdf +182785 -0
  29. data/data/solar.dat +36501 -0
  30. data/data/solarsystem.txt +17 -0
  31. data/data/suits.txt +1 -0
  32. data/data/wordlist.txt +210653 -0
  33. data/lib/bitlab.rb +624 -0
  34. data/lib/elizalab.rb +523 -0
  35. data/lib/encryptionlab.rb +42 -0
  36. data/lib/hashlab.rb +224 -0
  37. data/lib/introlab.rb +14 -0
  38. data/lib/iterationlab.rb +130 -0
  39. data/lib/randomlab.rb +294 -0
  40. data/lib/recursionlab.rb +228 -0
  41. data/lib/rubylabs.rb +507 -0
  42. data/lib/sievelab.rb +58 -0
  43. data/lib/sortlab.rb +213 -0
  44. data/lib/spherelab.rb +352 -0
  45. data/lib/temps.rb +41 -0
  46. data/lib/tsplab.rb +416 -0
  47. data/lib/viewer.rb +65 -0
  48. data/test/bit_test.rb +175 -0
  49. data/test/encryption_test.rb +20 -0
  50. data/test/iteration_test.rb +40 -0
  51. data/test/random_test.rb +64 -0
  52. data/test/recursion_test.rb +47 -0
  53. data/test/rubylabs_test.rb +18 -0
  54. data/test/sieve_test.rb +28 -0
  55. data/test/sphere_test.rb +130 -0
  56. data/test/temps_test.rb +24 -0
  57. data/test/test_helper.rb +18 -0
  58. metadata +112 -0
@@ -0,0 +1,64 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'test_helper'
3
+
4
+ include RandomLab
5
+
6
+ class TestRandoms < Test::Unit::TestCase
7
+
8
+ # For the Card tests, a call to the Card constructor with an integer argument
9
+ # makes a specified card, starting with 0 = A ♠ and ending with 51 = 2 ♣.
10
+
11
+ # Make a full deck, make sure every card is present. Then shuffle the deck,
12
+ # check again. Sort the shuffled deck, make sure all cards are there and in
13
+ # order.
14
+
15
+ def test_01_decks
16
+ deck1 = deal((0..51).to_a)
17
+ check_all_cards(deck1)
18
+ deck2 = shuffle
19
+ check_all_cards(deck2)
20
+ deck3 = deck2.sort
21
+ check_all_cards(deck3)
22
+ assert_equal deck1, deck3
23
+ end
24
+
25
+ # Make one of each kind of poker hand.
26
+
27
+ def test_02_poker_hands
28
+ print "\n poker hands"
29
+ assert_equal poker_hand(deal([0,1,2,3,4])), :straight_flush
30
+ assert_equal poker_hand(deal([0,13,26,39,1])), :four_of_a_kind
31
+ assert_equal poker_hand(deal([0,13,26,1,14])), :full_house
32
+ assert_equal poker_hand(deal([0,2,4,6,8])), :flush
33
+ assert_equal poker_hand(deal([0,14,2,3,4])), :straight
34
+ assert_equal poker_hand(deal([0,13,26,1,15])), :three_of_a_kind
35
+ assert_equal poker_hand(deal([0,13,1,14,15])), :two_pair
36
+ assert_equal poker_hand(deal([0,13,1,2,3])), :pair
37
+ assert_equal poker_hand(deal([0,14,29,44,45])), :nada
38
+ end
39
+
40
+
41
+ =begin rdoc
42
+ The +bins+ method makes a hash using keys based on suits or ranks
43
+ and initialized with a count of 0.
44
+
45
+ +check_all_cards+ makes sure there are 13 of each suit and 4 of each rank.
46
+ =end
47
+
48
+ def bins(a)
49
+ h = Hash.new
50
+ a.each do |x|
51
+ h[x] = 0
52
+ end
53
+ return h
54
+ end
55
+
56
+ def check_all_cards(a)
57
+ rb = bins(Card::Ranks)
58
+ sb = bins(Card::Suits)
59
+ a.each { |x| rb[x.rank] += 1; sb[x.suit] += 1 }
60
+ rb.each { |x,n| assert_equal n, 4 }
61
+ sb.each { |x,n| assert_equal n, 13 }
62
+ end
63
+
64
+ end
@@ -0,0 +1,47 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'test_helper'
3
+
4
+ include RecursionLab
5
+
6
+ class TestRecursiveAlgoritms < Test::Unit::TestCase
7
+
8
+ # Make some test arrays, try successful and unsuccessful searches.
9
+ # Test array sizes students will use (one less than power of two)
10
+ # plus some odd ones.
11
+
12
+ def test_01_bsearch
13
+ print "\n bsearch"
14
+ [14, 15, 16].each do |size|
15
+ a = TestArray.new(size).sort
16
+ assert_not_nil bsearch(a, a.random(:success))
17
+ assert_nil bsearch(a, a.random(:fail))
18
+ end
19
+ end
20
+
21
+ # Students will run tests with array sizes that are a power of two,
22
+ # but run some tests on other sizes, too. Also checks to make sure
23
+ # the array is not modified
24
+
25
+ def test_02_msort
26
+ print "\n msort"
27
+ [15, 16, 17].each do |size|
28
+ a = TestArray.new(size)
29
+ b = a.dup
30
+ assert_equal msort(a), a.sort
31
+ assert_equal a, b
32
+ end
33
+ end
34
+
35
+ # Array size not a factor here -- just try a few sorts
36
+
37
+ def test_03_qsort
38
+ print "\n qsort"
39
+ [15, 16, 17].each do |size|
40
+ a = TestArray.new(size)
41
+ b = a.dup
42
+ assert_equal qsort(a), a.sort
43
+ assert_equal a, b
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'test_helper'
3
+
4
+ class RubyLabsTest < Test::Unit::TestCase
5
+
6
+ def test_00_arrays
7
+ print "\n test_array"
8
+ a = TestArray.new(10)
9
+ assert a.length == 10
10
+ assert a.include?(a.random(:success))
11
+ assert !a.include?(a.random(:fail))
12
+ end
13
+
14
+ def test_01_scope
15
+ print "\n RubyScope [TBD]"
16
+ end
17
+
18
+ end
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'test_helper'
3
+
4
+ include SieveLab
5
+
6
+ class TestSieve < Test::Unit::TestCase
7
+
8
+ # Compare list generated by sieve method with list from Wikipedia. Test both the main
9
+ # version (sieve) and the simple prototype that iterates until the worklist is empty.
10
+
11
+ def test_01_first_25
12
+ print "\n sieve"
13
+ 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
+ assert_equal expected, sieve(100)
15
+ assert_equal expected, proto_sieve(100)
16
+ end
17
+
18
+ # Check length of list generated by sieve with expected number of primes (also from Wikipedia).
19
+ # pi[i] is the number of primes less than 10**i. Note: calling sieve(n) with n less than 2
20
+ # should quietly return an empty list.
21
+
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
28
+ end
@@ -0,0 +1,130 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'test_helper'
3
+
4
+ include SphereLab
5
+
6
+ =begin rdoc
7
+ This test file includes simple unit tests for the Vector and Body classes used in
8
+ the n-body simulation chapter, plus a more complicated test that should really be
9
+ an integration test. The complicated test is a mini-simulation of the movement
10
+ of the four inner planets over a period of 88 days (enough time for Mercury to make
11
+ a complete orbit of the sun). To see a plot of the simulation results run this
12
+ script with a file name as the command line argument, then run an R script (which
13
+ should be included in the tests directory) that will read the data and generate a plot:
14
+ % ruby sphere_test.rb nbody.out
15
+ % Rscript nbody.R
16
+ % open nbody.png
17
+ =end
18
+
19
+ class TestSpheres < Test::Unit::TestCase
20
+
21
+ # Vector arithmetic. Note: calls to assert_equal will also test the == method.
22
+
23
+ def test_01_vectors
24
+ print "\n vectors"
25
+
26
+ v1 = Vector.new(1, 1, 1)
27
+ v2 = Vector.new(2, 2, 2)
28
+ v3 = Vector.new(3, 3, 3)
29
+
30
+ assert_equal v3, v1 + v2
31
+ assert_equal v1, v3 - v2
32
+ assert_equal v2, v1 * 2
33
+
34
+ v3.add(v1)
35
+ assert_equal v3, Vector.new(4, 4, 4)
36
+ v3.sub(v1)
37
+ assert_equal v3, Vector.new(3, 3, 3)
38
+
39
+ assert_in_delta v2.norm, Math.sqrt(12), Float::EPSILON
40
+ end
41
+
42
+ # Body objects -- make two of them, test calculation of force attraction between them,
43
+ # verify movement is equal magnitude and in opposite direction (i.e. bodies move toward
44
+ # each other on the y axis).
45
+
46
+ def test_02_bodies
47
+ print "\n bodies"
48
+ y1 = 1000
49
+ y2 = 0
50
+ b1 = Body.new(1000000, Vector.new(0, y1, 0), Vector.new(0, 0, 0))
51
+ b2 = Body.new(1000000, Vector.new(0, y2, 0), Vector.new(0, 0, 0))
52
+ b1.add_force(b2)
53
+ b2.add_force(b1)
54
+ b1.move(100)
55
+ b2.move(100)
56
+ assert_equal b1.position.x, 0.0
57
+ assert_equal b1.position.x, 0.0
58
+ assert_in_delta (y1 - b1.position.y), (b2.position.y - y2), 1e-10
59
+ assert_equal b1.position.z, 0.0
60
+ assert_equal b1.position.z, 0.0
61
+ end
62
+
63
+ # Same as above, but use the class method (which does the calculation once and
64
+ # uses the same force twice)
65
+
66
+ def test_03_interaction
67
+ print "\n interaction"
68
+ y1 = 1000
69
+ y2 = 0
70
+ b1 = Body.new(1000000, Vector.new(0, y1, 0), Vector.new(0, 0, 0))
71
+ b2 = Body.new(1000000, Vector.new(0, y2, 0), Vector.new(0, 0, 0))
72
+ Body.interaction(b1, b2)
73
+ b1.move(100)
74
+ b2.move(100)
75
+ assert_equal b1.position.x, 0.0
76
+ assert_equal b1.position.x, 0.0
77
+ assert_in_delta (y1 - b1.position.y), (b2.position.y - y2), 1e-10
78
+ assert_equal b1.position.z, 0.0
79
+ assert_equal b1.position.z, 0.0
80
+ end
81
+
82
+ # Test the Body class by doing a mini-simulation of bodies based on the sun and
83
+ # inner planets of the solar system. Uses coordinates based on the JPL ephemeris
84
+ # for Jan 1 1970. Run for 88 time steps, with dt = 1 day. Expect mercury to be
85
+ # close to where it started, and earth to be about 1/4 through its orbit.
86
+
87
+ def test_04_solar_system
88
+ print "\n solar system"
89
+
90
+ bodies = [
91
+ Body.new(1.989E30, Vector.new(0,0.000E00,0), Vector.new(0.000E0,0,0), "sun"),
92
+ Body.new(3.303E23, Vector.new(3.83E+10, 2.87E+10, -1.17E+09), Vector.new(-38787.67, 41093.05, 6918.461), "mercury"),
93
+ Body.new(4.870E24, Vector.new(-5.37E+09, -1.08E+11, -1.16E+09), Vector.new(34741.48, -1865.747, -2031.506), "venus"),
94
+ Body.new(5.976E24, Vector.new(-2.70E+10, 1.44E+11, 9686451), Vector.new(-29770.44, -5568.042, 0.3961261), "earth"),
95
+ Body.new(6.421E23, Vector.new(1.98E+11, 7.42E+10, -3.33E+09), Vector.new(-7557.626, 24761.27, 704.7457), "mars")
96
+ ]
97
+
98
+ mercury = bodies[1]
99
+
100
+ dt = 86459 # number of seconds in a day (at 365.25 days/year)
101
+ nb = bodies.length # number of bodies
102
+ nt = 88 # number of time steps (simulated days); 88 is enough for 1 orbit for Mercury
103
+
104
+ myfirst = mercury.position.clone
105
+
106
+ nt.times do |t| # main simulation loop
107
+
108
+ for i in 0...nb # compute all pairwise interactions
109
+ for j in (i+1)...nb
110
+ Body.interaction( bodies[i], bodies[j] )
111
+ end
112
+ end
113
+
114
+ bodies.each do |b|
115
+ b.move(dt) # apply the accumulated forces
116
+ b.clear_force # reset force to 0 for next round
117
+ end
118
+
119
+ end
120
+
121
+ # Check to see if Mercury is close to where it started...
122
+
123
+ if nt == 88
124
+ assert_in_delta myfirst.x, mercury.position.x, 1e10
125
+ assert_in_delta myfirst.y, mercury.position.y, 1e10
126
+ assert_in_delta myfirst.z, mercury.position.z, 1e10
127
+ end
128
+ end
129
+
130
+ end
@@ -0,0 +1,24 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'test_helper'
3
+
4
+ include Temps
5
+
6
+ class TestTemps < Test::Unit::TestCase
7
+
8
+ # Check a few Faherenheit-to-Celsius conversions
9
+
10
+ def test_01_celsius
11
+ print "\n celsius"
12
+ assert_equal 100, celsius(212)
13
+ assert_equal 0, celsius(32)
14
+ assert_equal 26, celsius(80)
15
+ assert_equal "26.6666", celsius(80.0).to_s.slice(0..6)
16
+ end
17
+
18
+ # The fahrenheit method is a stub...
19
+
20
+ def test_02_fahrenheit
21
+ print "\n fahrenheit"
22
+ assert_nil fahrenheit(100)
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ # require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'rubylabs'
7
+
8
+ class Test::Unit::TestCase
9
+
10
+ def max(a, b)
11
+ return (a > b) ? a : b
12
+ end
13
+
14
+ def min(a, b)
15
+ return (a < b) ? a : b
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubylabs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.4
5
+ platform: ruby
6
+ authors:
7
+ - conery
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-24 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A set of modules for interactive experiments in an introductory computer science class.
17
+ email: conery@cs.uoregon.edu
18
+ executables:
19
+ - bb.rb
20
+ - statistics2.rb
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - LICENSE
25
+ - README.rdoc
26
+ files:
27
+ - LICENSE
28
+ - README.rdoc
29
+ - Rakefile
30
+ - VERSION
31
+ - bin/bb.rb
32
+ - bin/statistics2-0.53/ext/extconf.rb
33
+ - bin/statistics2-0.53/ext/show.rb
34
+ - bin/statistics2-0.53/ext/t.rb
35
+ - bin/statistics2-0.53/mklist.rb
36
+ - bin/statistics2-0.53/sample-tbl.rb
37
+ - bin/statistics2-0.53/statistics2.rb
38
+ - bin/statistics2-0.53/t-inv.rb
39
+ - bin/statistics2.rb
40
+ - data/aafreq.txt
41
+ - data/cars.txt
42
+ - data/century.txt
43
+ - data/colors.txt
44
+ - data/earth.yaml
45
+ - data/fruit.txt
46
+ - data/hacodes.txt
47
+ - data/hafreq.txt
48
+ - data/hvfreq.txt
49
+ - data/nbody.R
50
+ - data/nbody.out
51
+ - data/nbody.pdf
52
+ - data/nbody.png
53
+ - data/nbody3d.pdf
54
+ - data/outer.pdf
55
+ - data/solar.dat
56
+ - data/solarsystem.txt
57
+ - data/suits.txt
58
+ - data/wordlist.txt
59
+ - lib/bitlab.rb
60
+ - lib/elizalab.rb
61
+ - lib/encryptionlab.rb
62
+ - lib/hashlab.rb
63
+ - lib/introlab.rb
64
+ - lib/iterationlab.rb
65
+ - lib/randomlab.rb
66
+ - lib/recursionlab.rb
67
+ - lib/rubylabs.rb
68
+ - lib/sievelab.rb
69
+ - lib/sortlab.rb
70
+ - lib/spherelab.rb
71
+ - lib/temps.rb
72
+ - lib/tsplab.rb
73
+ - lib/viewer.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/conery/rubylabs
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
95
+ requirements: []
96
+
97
+ rubyforge_project: rubylabs
98
+ rubygems_version: 1.3.5
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Software and data for lab projects in the Science of Computing text.
102
+ test_files:
103
+ - test/bit_test.rb
104
+ - test/encryption_test.rb
105
+ - test/iteration_test.rb
106
+ - test/random_test.rb
107
+ - test/recursion_test.rb
108
+ - test/rubylabs_test.rb
109
+ - test/sieve_test.rb
110
+ - test/sphere_test.rb
111
+ - test/temps_test.rb
112
+ - test/test_helper.rb