/tutorial/gcd/test_gcd.e

http://github.com/tybor/Liberty · Specman e · 53 lines · 43 code · 4 blank · 6 comment · 1 complexity · 5eaf82d198ac6063650f53640754ef69 MD5 · raw file

  1. class TEST_GCD
  2. --
  3. -- Have a look at feature `gcd' to have an example of assertions in a loop.
  4. -- Also note that SmartEiffel handle recursivity in assertions.
  5. -- (There is also a feature `gcd' in class INTEGER_GENERAL.)
  6. --
  7. creation {ANY}
  8. make
  9. feature {ANY}
  10. make is
  11. do
  12. check
  13. gcd(3, 4) = 1
  14. gcd(4, 4) = 4
  15. gcd(8, 4) = 4
  16. gcd(9, 8) = 1
  17. gcd(9, 12) = 3
  18. end
  19. end
  20. gcd (value_1, value_2: INTEGER): INTEGER is
  21. -- Great Common Divisor of `value_1' and `value_2'.
  22. require
  23. value_1 > 0
  24. value_2 > 0
  25. local
  26. value: INTEGER
  27. do
  28. from
  29. Result := value_1
  30. value := value_2
  31. invariant
  32. Result > 0
  33. value > 0
  34. gcd(Result, value) = gcd(value_1, value_2)
  35. variant
  36. Result.max(value)
  37. until
  38. Result = value
  39. loop
  40. if Result > value then
  41. Result := Result - value
  42. else
  43. value := value - Result
  44. end
  45. end
  46. ensure
  47. Result = gcd(value_2, value_1)
  48. end
  49. end -- class TEST_GCD