fork download
  1. # Copy/paste of koans assert code
  2. #----------------------------------------------------
  3. FailedAssertionError = Class.new(StandardError)
  4.  
  5. def flunk(msg)
  6. raise FailedAssertionError, msg
  7. end
  8.  
  9. def assert(condition, msg=nil)
  10. msg ||= "Failed assertion."
  11. flunk(msg) unless condition
  12. true
  13. end
  14.  
  15. def assert_not_equal(expected, actual, msg=nil)
  16. msg ||= "Expected #{expected.inspect} to not equal #{actual.inspect}"
  17. assert(expected != actual, msg)
  18. end
  19. #----------------------------------------------------
  20.  
  21. # so for this let's say that you're not modeling dice, you're moding something that returns a randomized set of N
  22. # values for an array, and it's normalized, from [0.0 to 1.0] in value
  23.  
  24.  
  25. # This set always returns [0,0,0,0,...]
  26. class FalsePositiveDiceSet
  27. attr_reader :values
  28.  
  29. def roll(numberToRoll)
  30. #oops, I meant to type rand(1.0)
  31. @values = Array.new(numberToRoll){Random.rand(1)}
  32. end
  33. end
  34.  
  35. # This set returns something like [0.12870590894540357, 0.1339125315088633, 0.256765300105959, ...]
  36. class TruePositiveDiceSet
  37. attr_reader :values
  38.  
  39. def roll(numberToRoll)
  40. @values = Array.new(numberToRoll){Random.rand(1.0)}
  41. end
  42.  
  43.  
  44. end
  45.  
  46. class TrueNegativeDiceSet
  47. attr_reader :values
  48.  
  49. def initialize()
  50. @values = [0.0, 0.0, 0.0]
  51. end
  52.  
  53. def roll(numberToRoll)
  54. #whoops, I forgot to roll
  55. end
  56. end
  57.  
  58. # false negative implementation
  59. class SingleDiceArrayGoodSet
  60. attr_reader :values
  61.  
  62. # Alternative, but still technically valid implementation of the same class.
  63. # If you knew you only wanted to have one set of dice at a time, per class,
  64. # you could store the array in a field, and just return the same array every time,
  65. # though I generally wouldn't recommend this unless you have a good reason to, it's weirder to use.
  66. def initialize()
  67. @values = []
  68. end
  69.  
  70. def roll(numberToRoll)
  71. @values.clear
  72. numberToRoll.times{@values << Random.rand(1.0)}
  73. end
  74.  
  75. end
  76.  
  77. # test code
  78. #----------------------------------------------------
  79.  
  80. def TestDiceObject(dice)
  81. dice.roll(3)
  82. first_time = dice.values
  83. puts("First roll is #{dice.values}")
  84. dice.roll(3)
  85. puts("Second roll is #{dice.values}")
  86.  
  87. # NOTE: for SingleDiceArrayGoodSet, since it's the same array first_time and second_time are the same array.
  88. second_time = dice.values
  89.  
  90. # breaking up the statement into parts
  91. left = [first_time, first_time.object_id]
  92. right = [second_time, second_time.object_id]
  93. puts("first time side of assert is #{left.inspect}")
  94. puts("second time side of assert is #{right.inspect}")
  95.  
  96. assert_statement = (left != right)
  97. puts("Assert statement is: #{assert_statement}")
  98.  
  99. puts("first_time and second_time object_ids do not match: #{first_time.object_id != second_time.object_id}")
  100.  
  101. begin
  102. assert_not_equal [first_time, first_time.object_id], [second_time, second_time.object_id], "Two rolls should not be equal"
  103. rescue FailedAssertionError => ex
  104. puts("[NOT OK] This dice set was detected as bad by the unit tests. Assert tripped with error: #{ex.to_s}")
  105. else
  106. puts("[OK] The unit tests think this dice set is good!")
  107. end
  108.  
  109. end
  110.  
  111. puts("True positive dice set:")
  112. puts("-------------------------------------------------------------------------------")
  113. TestDiceObject(TruePositiveDiceSet.new)
  114. puts
  115. puts("True negative dice set:")
  116. puts("-------------------------------------------------------------------------------")
  117. TestDiceObject(TrueNegativeDiceSet.new)
  118. puts
  119. puts("False positive dice set:")
  120. puts("-------------------------------------------------------------------------------")
  121. puts
  122. TestDiceObject(FalsePositiveDiceSet.new)
  123. puts
  124. puts("False Negative SingleDiceArrayGoodSet dice set:")
  125. puts("-------------------------------------------------------------------------------")
  126. puts
  127. TestDiceObject(SingleDiceArrayGoodSet.new)
  128.  
  129. # So as other answers have mentioned, it would be pretty hard to test for this kind of error.
  130. # I'd personally run through the program and read its "random" values for a quick check to make sure something like this didn't happen.
Success #stdin #stdout 0.01s 6448KB
stdin
Standard input is empty
stdout
True positive dice set:
-------------------------------------------------------------------------------
First roll is [0.9295555667775741, 0.023332453305303646, 0.8090767557897749]
Second roll is [0.986046019962342, 0.7535232540495751, 0.9338589841699384]
first time side of assert is [[0.9295555667775741, 0.023332453305303646, 0.8090767557897749], 47041683158400]
second time side of assert is [[0.986046019962342, 0.7535232540495751, 0.9338589841699384], 47041683158220]
Assert statement is: true
first_time and second_time object_ids do not match: true
[OK] The unit tests think this dice set is good!

True negative dice set:
-------------------------------------------------------------------------------
First roll is [0.0, 0.0, 0.0]
Second roll is [0.0, 0.0, 0.0]
first time side of assert is [[0.0, 0.0, 0.0], 47041683157360]
second time side of assert is [[0.0, 0.0, 0.0], 47041683157360]
Assert statement is: false
first_time and second_time object_ids do not match: false
[NOT OK] This dice set was detected as bad by the unit tests. Assert tripped with error: Two rolls should not be equal

False positive dice set:
-------------------------------------------------------------------------------

First roll is [0, 0, 0]
Second roll is [0, 0, 0]
first time side of assert is [[0, 0, 0], 47041683156360]
second time side of assert is [[0, 0, 0], 47041683156220]
Assert statement is: true
first_time and second_time object_ids do not match: true
[OK] The unit tests think this dice set is good!

False Negative SingleDiceArrayGoodSet dice set:
-------------------------------------------------------------------------------

First roll is [0.37735903981889907, 0.7953462298828735, 0.9627567868684141]
Second roll is [0.40507424290032346, 0.03615654611430941, 0.20780610926976506]
first time side of assert is [[0.40507424290032346, 0.03615654611430941, 0.20780610926976506], 47041683155400]
second time side of assert is [[0.40507424290032346, 0.03615654611430941, 0.20780610926976506], 47041683155400]
Assert statement is: false
first_time and second_time object_ids do not match: false
[NOT OK] This dice set was detected as bad by the unit tests. Assert tripped with error: Two rolls should not be equal