PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 1class 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
 8creation {ANY}
 9	make
10
11feature {ANY}
12	make is
13		do
14			check
15				gcd(3, 4) = 1
16				gcd(4, 4) = 4
17				gcd(8, 4) = 4
18				gcd(9, 8) = 1
19				gcd(9, 12) = 3
20			end
21		end
22
23	gcd (value_1, value_2: INTEGER): INTEGER is
24			-- Great Common Divisor of `value_1' and `value_2'.
25		require
26			value_1 > 0
27			value_2 > 0
28		local
29			value: INTEGER
30		do
31			from
32				Result := value_1
33				value := value_2
34			invariant
35				Result > 0
36				value > 0
37				gcd(Result, value) = gcd(value_1, value_2)
38			variant
39				Result.max(value)
40			until
41				Result = value
42			loop
43				if Result > value then
44					Result := Result - value
45				else
46					value := value - Result
47				end
48			end
49		ensure
50			Result = gcd(value_2, value_1)
51		end
52
53end -- class TEST_GCD