PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ch9/test-fuzzy.rb

https://github.com/tcopeland/ai-app-prog
Ruby | 85 lines | 81 code | 3 blank | 1 comment | 3 complexity | 78aec7c35ae3cce8f9e1c47a8f50231c MD5 | raw file
  1. #!/usr/local/bin/ruby
  2. require 'test/unit'
  3. require 'fuzzy.rb'
  4. class TimerTest < Test::Unit::TestCase
  5. def test_bump
  6. t = Timer.new
  7. t.bump
  8. assert(t.elapsed == 1, "Timer.bump didn't increase elapsed time")
  9. end
  10. def test_reset
  11. t = Timer.new
  12. t.bump
  13. t.reset
  14. assert(t.elapsed == 0, "Timer.reset didn't zero elapsed time")
  15. end
  16. end
  17. class ConstrainedValueTest < Test::Unit::TestCase
  18. def test_simple
  19. v = ConstrainedValue.new(5, 0.0, 35.0)
  20. assert(v.current == 5, "Constructor didn't set values")
  21. v.add 3
  22. assert(v.current == 8, "ConstrainedValue.add busted")
  23. v.subtract 4
  24. assert(v.current == 4, "ConstrainedValue.subtract busted")
  25. end
  26. def test_constrain_high
  27. v = ConstrainedValue.new(36, 0.0, 35.0)
  28. v.constrain
  29. assert(v.current == 35.0, "ConstrainedValue.constrain didn't reign in high value")
  30. end
  31. def test_constrain_low
  32. v = ConstrainedValue.new(-2.0, 0.0, 35.0)
  33. v.constrain
  34. assert(v.current == 0.0, "ConstrainedValue.constrain didn't reign in low value")
  35. end
  36. end
  37. class BatteryMembershipFunctionsTest < Test::Unit::TestCase
  38. def test_low
  39. b = BatteryMembershipFunctions.new
  40. assert(b.low.compute(ConstrainedValue.new(1.0, 0.0, 35.0).current) == 1.0, "Voltage below low end should have resulted in 1.0")
  41. assert(b.low.compute(ConstrainedValue.new(11.0, 0.0, 35.0).current) == 0.0, "Voltage above high end should have resulted in 0.0")
  42. end
  43. def test_medium
  44. b = BatteryMembershipFunctions.new
  45. assert(b.medium.compute(ConstrainedValue.new(2.0, 0.0, 35.0).current) == 0.0, "Voltage below low end should have resulted in 0.0")
  46. assert(b.medium.compute(ConstrainedValue.new(30.0, 0.0, 35.0).current) == 0.0, "Voltage above high end should have resulted in 0.0")
  47. end
  48. def test_high
  49. b = BatteryMembershipFunctions.new
  50. assert(b.high.compute(ConstrainedValue.new(20.0, 0.0, 35.0).current) == 0.0, "Voltage below low end should have resulted in 0.0")
  51. assert(b.high.compute(ConstrainedValue.new(35.0, 0.0, 35.0).current) == 1.0, "Voltage above high end should have resulted in 1.0")
  52. end
  53. end
  54. class SpikeProfileTest < Test::Unit::TestCase
  55. def test_simple
  56. p = SpikeProfile.new(2,5)
  57. assert(p.compute(3).to_s[0..3] == "0.66", "Spike profile failed for value close to low")
  58. p = SpikeProfile.new(-5,5)
  59. assert(p.compute(4).to_s[0..2] == "0.2", "Spike profile failed for value close to high with negative low")
  60. p = SpikeProfile.new(-4,-1)
  61. assert(p.compute(-5).to_s[0..3] == "1.33", "Spike profile failed for lots of negative values")
  62. end
  63. end
  64. class FuzzyOperationsTest < Test::Unit::TestCase
  65. def test_and
  66. f = FuzzyOperations.new
  67. assert(f.and(2,3) == 3, "Fuzzy and should result in larger value")
  68. end
  69. def test_or
  70. f = FuzzyOperations.new
  71. assert(f.or(2,3) == 2, "Fuzzy or should result in smaller value")
  72. end
  73. def test_not
  74. f = FuzzyOperations.new
  75. assert(f.not(0.2) == 0.8, "Fuzzy not should result in 1.0 - value")
  76. end
  77. end