Skip to content
Browse files

some refactoring, new tests, and bug fixes

  • Loading branch information...
1 parent 3b6f11a commit 808e5806006762f339f124aae3e110985900eecf Richard Colley committed Feb 26, 2010
Showing with 184 additions and 22 deletions.
  1. +16 −11 multiplication_table.rb
  2. +168 −11 test_mult.rb
View
27 multiplication_table.rb
@@ -5,18 +5,18 @@
=end
def indent( text, cols )
- text.split(/\r|\r\n|\n/).map {|line| " "*cols << line}
+ text.split(/\r|\r\n|\n/).map {|line| " "*cols << line}.join($/) << $/
end
-def make_heading( heading, width=0 )
- heading.empty? ? "" : heading.center(width).rstrip << $/
+def make_heading( heading, width=0, centre=false )
+ (heading==nil) ? "" : heading.center(centre ? width : 0).rstrip << $/
end
def make_decoration(width)
"=" * width << $/
end
-def multiplication_table( integer, heading = '', decorate = false)
+def multiplication_table( integer, heading = '', decorate = false, centre = false)
times_table = []
if integer == 0
times_table << [0]
@@ -34,7 +34,7 @@ def multiplication_table( integer, heading = '', decorate = false)
other_column_width = max_value.to_s.size
table_width = first_column_width + 1 + (other_column_width+1) * (times_table.size-1)
- heading_str = make_heading(heading, table_width)
+ heading_str = make_heading(heading, table_width, centre)
decoration = decorate ? make_decoration(table_width) : ""
table_str = times_table.inject("") do |acc,row|
row.each_with_index do |v,i|
@@ -54,30 +54,35 @@ def multiplication_table( integer, heading = '', decorate = false)
def usage(msg=nil)
puts msg if msg
- puts "Usage: #{$0} [-d] [-h] <number> [<number>...]"
+ puts "Usage: #{$0} [-d] [-h <heading_text>] [-c] <number> [<number>...]"
puts <<-EOF
Prints times tables for the given numbers.
If -d is given, some table decoration is applied.
If -h is given, a heading is applied to each table.
+ In the <heading_text> '$' will be replaced with the table number.
+ If -c is given, any heading is centred on the table.
EOF
exit(1)
end
opts = GetoptLong.new(
[ "--decorate", "-d", GetoptLong::NO_ARGUMENT ],
- [ "--heading", "-h", GetoptLong::NO_ARGUMENT ]
+ [ "--heading", "-h", GetoptLong::REQUIRED_ARGUMENT ],
+ [ "--centre", "-c", GetoptLong::NO_ARGUMENT ]
)
-opt_decorate = opt_heading = false
+opt_decorate = opt_heading = opt_centre = false
+heading_template=nil
opts.each do |opt,arg|
case opt
when '--decorate' then opt_decorate = true
- when '--heading' then opt_heading = true
- else usage "Unknown option '#{opt}'"
+ when '--heading' then opt_heading = true ; heading_template=arg
+ when '--centre' then opt_centre = true
end
end rescue usage
while i = ARGV.shift
- puts multiplication_table(i.to_i, opt_heading ? "x#{i}" : "", opt_decorate)
+ heading = opt_heading ? heading_template.gsub(/\$/,"#{i}") : nil
+ puts multiplication_table(i.to_i, heading, opt_decorate, opt_centre)
puts unless ARGV.empty?
end
View
179 test_mult.rb
@@ -2,9 +2,61 @@
require 'test/unit'
class TC_Table < Test::Unit::TestCase
+ def run_test( expected_result, *args )
+ assert_equal expected_result, multiplication_table( *args )
+ end
+
def test_table_12
-
-result_12 = <<-EOS
+ expected_result, args = setup_test_table_12
+ run_test( expected_result, *args )
+ end
+
+ def test_table_3
+ expected_result, args = setup_test_table_3
+ run_test( expected_result, *args )
+ end
+
+ def test_table_1
+ expected_result, args = setup_test_table_1
+ run_test( expected_result, *args )
+ end
+
+ def test_many
+ expected_result, tables = setup_test_many
+ result_str = ""
+ tables.each_with_index do |v,i|
+ result_str << $/ unless i == 0
+ result_str << multiplication_table( v, "#{v}x#{v}", true )
+ end
+ assert_equal expected_result, result_str
+ end
+
+ def test_centre_5
+ expected_result, args = setup_test_centre_5
+ run_test( expected_result, *args )
+ end
+
+ def test_no_heading
+ expected_result, args = setup_test_no_heading
+ run_test( expected_result, *args )
+ end
+
+ def test_blank_heading
+ expected_result, args = setup_test_blank_heading
+ run_test( expected_result, *args )
+ end
+
+ def test_default_heading
+ expected_result, args = setup_test_default_heading
+ run_test( expected_result, *args )
+ end
+
+
+########################################################
+
+ def setup_test_table_12
+ args = [12, 'Table to 12', true]
+ result = <<EOS
Table to 12
================================================
1 2 3 4 5 6 7 8 9 10 11 12
@@ -21,19 +73,124 @@ def test_table_12
12 24 36 48 60 72 84 96 108 120 132 144
================================================
EOS
+ return result, args
-result_1 = " \n ===\n 1\n ===\n"
-result_3 = " \n 1 2 3\n 2 4 6\n 3 6 9\n"
+ end
- result12 = multiplication_table 12, 'Table to 12', true
- result1 = multiplication_table 1, '', true
- result3 = multiplication_table 3
- assert_equal result_12, result12
- assert_equal result_1, result1
- assert_equal result_3, result3
+ def setup_test_table_1
+ return " \n ===\n 1\n ===\n", [1, '', true]
+ end
+ def setup_test_table_3
+ return " \n 1 2 3\n 2 4 6\n 3 6 9\n", 3
end
-end
+ def setup_test_no_heading
+ return " ===\n 1\n ===\n", [1, nil, true]
+ end
+
+ def setup_test_blank_heading
+ return " \n ===\n 1\n ===\n", [1, '', true]
+ end
+
+ def setup_test_default_heading
+ return " \n 1 2 3\n 2 4 6\n 3 6 9\n", 3
+ end
+ def setup_test_centre_5
+ args = [5, '5x5', true, true]
+ expected_result = <<EOS
+ 5x5
+ ===============
+ 1 2 3 4 5
+ 2 4 6 8 10
+ 3 6 9 12 15
+ 4 8 12 16 20
+ 5 10 15 20 25
+ ===============
+EOS
+ return expected_result, args
+ end
+
+ def setup_test_many
+ tables = [0, 1, 2, 5, 10, 32]
+ expected_result = <<EOS
+ 0x0
+ ===
+ 0
+ ===
+
+ 1x1
+ ===
+ 1
+ ===
+
+ 2x2
+ =====
+ 1 2
+ 2 4
+ =====
+
+ 5x5
+ ===============
+ 1 2 3 4 5
+ 2 4 6 8 10
+ 3 6 9 12 15
+ 4 8 12 16 20
+ 5 10 15 20 25
+ ===============
+
+ 10x10
+ ========================================
+ 1 2 3 4 5 6 7 8 9 10
+ 2 4 6 8 10 12 14 16 18 20
+ 3 6 9 12 15 18 21 24 27 30
+ 4 8 12 16 20 24 28 32 36 40
+ 5 10 15 20 25 30 35 40 45 50
+ 6 12 18 24 30 36 42 48 54 60
+ 7 14 21 28 35 42 49 56 63 70
+ 8 16 24 32 40 48 56 64 72 80
+ 9 18 27 36 45 54 63 72 81 90
+ 10 20 30 40 50 60 70 80 90 100
+ ========================================
+
+ 32x32
+ ===============================================================================================================================================================
+ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
+ 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64
+ 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96
+ 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120 124 128
+ 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160
+ 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 156 162 168 174 180 186 192
+ 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210 217 224
+ 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160 168 176 184 192 200 208 216 224 232 240 248 256
+ 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171 180 189 198 207 216 225 234 243 252 261 270 279 288
+ 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320
+ 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 176 187 198 209 220 231 242 253 264 275 286 297 308 319 330 341 352
+ 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 192 204 216 228 240 252 264 276 288 300 312 324 336 348 360 372 384
+ 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 208 221 234 247 260 273 286 299 312 325 338 351 364 377 390 403 416
+ 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210 224 238 252 266 280 294 308 322 336 350 364 378 392 406 420 434 448
+ 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225 240 255 270 285 300 315 330 345 360 375 390 405 420 435 450 465 480
+ 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256 272 288 304 320 336 352 368 384 400 416 432 448 464 480 496 512
+ 17 34 51 68 85 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340 357 374 391 408 425 442 459 476 493 510 527 544
+ 18 36 54 72 90 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360 378 396 414 432 450 468 486 504 522 540 558 576
+ 19 38 57 76 95 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380 399 418 437 456 475 494 513 532 551 570 589 608
+ 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400 420 440 460 480 500 520 540 560 580 600 620 640
+ 21 42 63 84 105 126 147 168 189 210 231 252 273 294 315 336 357 378 399 420 441 462 483 504 525 546 567 588 609 630 651 672
+ 22 44 66 88 110 132 154 176 198 220 242 264 286 308 330 352 374 396 418 440 462 484 506 528 550 572 594 616 638 660 682 704
+ 23 46 69 92 115 138 161 184 207 230 253 276 299 322 345 368 391 414 437 460 483 506 529 552 575 598 621 644 667 690 713 736
+ 24 48 72 96 120 144 168 192 216 240 264 288 312 336 360 384 408 432 456 480 504 528 552 576 600 624 648 672 696 720 744 768
+ 25 50 75 100 125 150 175 200 225 250 275 300 325 350 375 400 425 450 475 500 525 550 575 600 625 650 675 700 725 750 775 800
+ 26 52 78 104 130 156 182 208 234 260 286 312 338 364 390 416 442 468 494 520 546 572 598 624 650 676 702 728 754 780 806 832
+ 27 54 81 108 135 162 189 216 243 270 297 324 351 378 405 432 459 486 513 540 567 594 621 648 675 702 729 756 783 810 837 864
+ 28 56 84 112 140 168 196 224 252 280 308 336 364 392 420 448 476 504 532 560 588 616 644 672 700 728 756 784 812 840 868 896
+ 29 58 87 116 145 174 203 232 261 290 319 348 377 406 435 464 493 522 551 580 609 638 667 696 725 754 783 812 841 870 899 928
+ 30 60 90 120 150 180 210 240 270 300 330 360 390 420 450 480 510 540 570 600 630 660 690 720 750 780 810 840 870 900 930 960
+ 31 62 93 124 155 186 217 248 279 310 341 372 403 434 465 496 527 558 589 620 651 682 713 744 775 806 837 868 899 930 961 992
+ 32 64 96 128 160 192 224 256 288 320 352 384 416 448 480 512 544 576 608 640 672 704 736 768 800 832 864 896 928 960 992 1024
+ ===============================================================================================================================================================
+EOS
+ return expected_result, tables
+ end
+end

0 comments on commit 808e580

Please sign in to comment.
Something went wrong with that request. Please try again.