note
	description: "Test MONEY and ACCOUNT classes."
	author: ""
	date: "$Date$"
	revision: "$Revision$"

class 
	TEST_FLOAT1

inherit
	ES_TEST

create 
	make

feature {NONE} -- Initialization

	make
			-- Run tests
		do
			add_boolean_case (agent ta0)
			add_boolean_case (agent t0)
			add_boolean_case (agent t1)
			add_boolean_case (agent t2)
			add_boolean_case (agent t3)
			add_boolean_case (agent t4)
			add_boolean_case (agent t5)
			add_boolean_case (agent t6)
			add_boolean_case (agent t7)
			add_boolean_case (agent t8)
			add_boolean_case (agent t9)
			add_boolean_case (agent t10)
			add_boolean_case (agent tb1)
			add_boolean_case (agent tb2)
			add_boolean_case (agent tb3)
		end
	
feature -- tests for NUM_ARRAY

	ta0: BOOLEAN
		local
			an: NUM_ARRAY [INTEGER_32]
		do
			comment ("ta0: test numeric array creation")
			an := create {attached NUM_ARRAY [INTEGER_32]}.from_array (<<3, 5, 7>>)
			Result := an.sum = 15
		end
	
feature -- money tests

	t0: BOOLEAN
		local
			r: FLOAT
			i: NATURAL_32
		do
			comment ("t0: in floating point, 0.1 + 0.1 + .. /= 1.0")
			from
				i := 0
				r := create {FLOAT}.make_from_real (0.0)
			until
				i = 10
			loop
				r := r + create {FLOAT}.make_from_real (0.1)
				i := i + 1
			end
			Result := r /= create {FLOAT}.make_from_real (1.0)
		end

	t1: BOOLEAN
		local
			m1, m2, m3: MONEY
			amount: FLOAT
			cents: INTEGER_64
		do
			comment ("t1: make $4.84 and $4.85 and former less than latter")
			create m1.make_with_float (create {FLOAT}.make_from_real (4.84))
			amount := m1.amount
			cents := m1.cents
			Result := amount = create {FLOAT}.make_from_real (4.84) and cents = 484
			check
					Result
			end
			create m2.make_with_float (create {FLOAT}.make_from_real (4.85))
			Result := m1 < m2
			check
					Result
			end
			create m3.make (4, 84)
			Result := m1 = m3
		end

	t2: BOOLEAN
		local
			m1, m2, m: MONEY
		do
			comment ("t2: make $4.84 and $4.85 and do arithmetic")
			create m1.make_with_float (create {FLOAT}.make_from_real (4.84))
			create m2.make_with_float (create {FLOAT}.make_from_real (4.85))
			create m.make_with_float (create {FLOAT}.make_from_real (9.69))
			Result := (m1 + m2) = m
			check
					Result
			end
			create m.make_with_float (create {FLOAT}.make_from_real (.01))
			Result := (m2 - m1) = m
			check
					Result
			end
			create m.make_with_float (create {FLOAT}.make_from_real (24.2484))
			Result := (m1 * create {FLOAT}.make_from_real (5.01)) = m
			check
					Result
			end
			Result := m1 / create {FLOAT}.make_from_real (5.01) = create {MONEY}.make_with_float (create {FLOAT}.make_from_real (0.966067864))
			check
					Result
			end
			Result := m1 / create {FLOAT}.make_from_real (5.01) = create {MONEY}.make_with_float (create {FLOAT}.make_from_real (0.97))
			check
					Result
			end
			Result := m1 / create {FLOAT}.make_from_real (5.01) > create {MONEY}.make_with_float (create {FLOAT}.make_from_real (0.96))
			check
					Result
			end
		end

	t3: BOOLEAN
		local
			divisions: MONEY_ARRAY
			m, base, larger: MONEY
		do
			comment ("t3: make $24.20 and do 6 equal allocations")
			create m.make_with_float (create {FLOAT}.make_from_real (24.20))
			create base.make_with_float (create {FLOAT}.make_from_real (4.03))
			create larger.make_with_float (create {FLOAT}.make_from_real (4.04))
			divisions := create {attached MONEY_ARRAY}.from_array (<<larger, larger, base, base, base, base>>)
			Result := m.allocated (6) ~ divisions
		end

	t4: BOOLEAN
		local
			divisions, ma: MONEY_ARRAY
			m, base, larger: MONEY
		do
			comment ("t4: make $24.20 and do 6 allocations according to equal ratios")
			create m.make_with_float (create {FLOAT}.make_from_real (24.20))
			create base.make_with_float (create {FLOAT}.make_from_real (4.03))
			create larger.make_with_float (create {FLOAT}.make_from_real (4.04))
			divisions := create {attached MONEY_ARRAY}.from_array (<<larger, larger, base, base, base, base>>)
			ma := m.allocated_by_ratios (create {NUM_ARRAY [INTEGER_32]}.from_array (<<1, 1, 1, 1, 1, 1>>))
			Result := ma ~ divisions
		end

	t5: BOOLEAN
		local
			divisions: MONEY_ARRAY
			m, base, one_cent, m1, m2, m3, m4: MONEY
		do
			comment ("t5: make $24.20 and do 4 allocations according to uneven, non-zero ratios")
			create m.make_with_float (create {FLOAT}.make_from_real (24.20))
			create one_cent.make_with_float (create {FLOAT}.make_from_real (.01))
			create base.make_with_float (create {FLOAT}.make_from_real (4.03))
			m1 := base * create {FLOAT}.make_from_integer_32 (1) + one_cent
			m2 := base * create {FLOAT}.make_from_integer_32 (2) + one_cent
			m3 := base * create {FLOAT}.make_from_integer_32 (2)
			m4 := base * create {FLOAT}.make_from_integer_32 (1)
			divisions := create {attached MONEY_ARRAY}.from_array (<<m1, m2, m3, m4>>)
			Result := m.allocated_by_ratios (create {NUM_ARRAY [INTEGER_32]}.from_array (<<1, 2, 2, 1>>)) ~ divisions
		end

	t6: BOOLEAN
		local
			divisions: MONEY_ARRAY
			m, one_cent, m1, m2, m3, m4: MONEY
		do
			comment ("t6: make $24.20 and do 4 allocations according to uneven, zero ratios")
			create m.make_with_float (create {FLOAT}.make_from_real (24.20))
			create one_cent.make_with_float (create {FLOAT}.make_from_real (.01))
			create m1.make_with_float (create {FLOAT}.make_from_real (4.04))
			create m2.make_with_float (create {FLOAT}.make_from_real (0.00))
			create m3.make_with_float (create {FLOAT}.make_from_real (8.06))
			create m4.make_with_float (create {FLOAT}.make_from_real (12.10))
			divisions := create {attached MONEY_ARRAY}.from_array (<<m1, m2, m3, m4>>)
			Result := m.allocated_by_ratios (create {NUM_ARRAY [INTEGER_32]}.from_array (<<1, 0, 2, 3>>)) ~ divisions
		end

	t7: BOOLEAN
		local
			m0, m1, m2, m3, m4: MONEY
		do
			comment ("t7: test MONEY: zero, one, /, *, unary plus and minus")
			create m1.make_from_int (0)
			create m1.make_from_int (1)
			Result := m0 = m0.zero and m1 = m0.one
			check
					Result
			end
			create m2.make_with_float (create {FLOAT}.make_from_real (1.00))
			create m3.make_with_float (create {FLOAT}.make_from_real (.33))
			Result := m2 / create {FLOAT}.make_from_integer_32 (3) = m3
			check
					Result
			end
			create m4.make_with_float (create {FLOAT}.make_from_real (3.37))
			Result := m3 * create {FLOAT}.make_from_real (10.2) = m4
			check
					Result
			end
			Result := - m4 = create {MONEY}.make_with_float (create {FLOAT}.make_from_real (-3.37))
			check
					Result
			end
			Result := + m4 = create {MONEY}.make_with_float (create {FLOAT}.make_from_real (3.37))
			check
					Result
			end
			Result := - m1.one = create {MONEY}.make_with_float (create {FLOAT}.make_from_real (-0.01)) and - m0 = m0.zero
		end

	t8: BOOLEAN
		local
			m1, m2, m3: MONEY
			ratio: MONEY_ARRAY
		do
			comment ("t8: allocate_by_ratio(<<15, 85>>) on $600.01 should yield $90.01 and $510")
			create m1.make_with_float (create {FLOAT}.make_from_real (600.01))
			create m2.make_with_float (create {FLOAT}.make_from_real (90.01))
			create m3.make_with_float (create {FLOAT}.make_from_integer_32 (510))
			ratio := m1.allocated_by_ratios (create {NUM_ARRAY [INTEGER_32]}.from_array (<<15, 85>>))
			Result := ratio [1] = m2 and ratio [2] = m3
			check
					Result
			end
			Result := ratio [1] + ratio [2] = m1
			check
					Result
			end
		end

	t9: BOOLEAN
		local
			m1, m2, m3: MONEY
			ratio: MONEY_ARRAY
		do
			comment ("t9: allocate_by_ratio(<<15, 85>>) on $600.01 should yield $90.01 and $510")
			create m1.make_with_float (create {FLOAT}.make_from_real (604.91))
			create m2.make_with_float (create {FLOAT}.make_from_real (90.74))
			create m3.make_with_float (create {FLOAT}.make_from_real (514.17))
			ratio := m1.allocated_by_ratios (create {NUM_ARRAY [INTEGER_32]}.from_array (<<15, 85>>))
			Result := ratio [1] = m2 and ratio [2] = m3
			check
					Result
			end
			Result := ratio [1] + ratio [2] = m1
			check
					Result
			end
		end

	t10: BOOLEAN
		local
			m, m1, m2, m3, m4, m5: MONEY
			allocations, divisions: MONEY_ARRAY
		do
			comment ("t10: allocate_by_ratio(<<1, 0, 4, 4, 4>>) on $24.20 should yield <<1.87, 0.01. 7.44, 7.44, 7.44>>")
			create m.make_with_float (create {FLOAT}.make_from_real (24.20))
			create m1.make_with_float (create {FLOAT}.make_from_real (1.86 + 0.01))
			create m2.make_with_float (create {FLOAT}.make_from_real (0.00 + 0.01))
			create m3.make_with_float (create {FLOAT}.make_from_real (7.44))
			create m4.make_with_float (create {FLOAT}.make_from_real (7.44))
			create m5.make_with_float (create {FLOAT}.make_from_real (7.44))
			divisions := create {attached MONEY_ARRAY}.from_array (<<m1, m2, m3, m4, m5>>)
			allocations := m.allocated_by_ratios (create {NUM_ARRAY [INTEGER_32]}.from_array (<<1, 0, 4, 4, 4>>))
			Result := allocations ~ divisions
		end
	
feature -- bank tests

	tb1: BOOLEAN
		local
			b: BANK
			m1, m2, m3, m4, m5: MONEY
		do
			comment ("tb1: test bank features: deposit, withdraw and transfer for 50%% of the balance")
			create b.make
			create m1.make_with_float (create {FLOAT}.make_from_integer_32 (700))
			create m2.make_with_float (create {FLOAT}.make_from_integer_32 (500))
			create m3.make_with_float (create {FLOAT}.make_from_integer_32 (100))
			b.account1.deposit (m1)
			b.account2.deposit (m2)
			Result := b.account1.balance = m1 and b.account2.balance = m2
			check
					Result
			end
			b.account1.withdraw (m3)
			Result := b.account1.balance = (m1 - m3) and b.account2.balance = m2
			check
					Result
			end
			create m4.make_with_float (create {FLOAT}.make_from_integer_32 (300))
			create m5.make_with_float (create {FLOAT}.make_from_integer_32 (800))
			b.account1.transfer (50, b.account2)
			Result := b.account1.balance = m4 and b.account2.balance = m5
			check
					Result
			end
		end

	tb2: BOOLEAN
		local
			b: BANK
			m1, m2, m3, m4, m5, total: MONEY
			na: MONEY_ARRAY
		do
			comment ("tb2: test bank transfer for 10%% of the balance where the amounts are unequal")
			create b.make
			create m1.make_with_float (create {FLOAT}.make_from_real (600.01))
			create m2.make_with_float (create {FLOAT}.make_from_real (500.01))
			b.account1.deposit (m1)
			total := total + m1
			b.account2.deposit (m2)
			total := total + m2
			Result := b.account1.balance = m1 and b.account2.balance = m2
			check
					Result
			end
			Result := b.account1.balance = (m1 - m3) and b.account2.balance = m2
			check
					Result
			end
			na := b.account1.balance.allocated_by_ratios (create {NUM_ARRAY [INTEGER_32]}.from_array (<<10, 90>>))
			create m3.make_with_float (create {FLOAT}.make_from_real (60.01))
			check
					m3 = na [1]
			end
			b.account1.transfer (10, b.account2)
			create m4.make_with_float (create {FLOAT}.make_from_integer_32 (540))
			create m5.make_with_float (create {FLOAT}.make_from_real (560.02))
			Result := b.account1.balance = m4 and b.account2.balance = m5
			check
					Result
			end
			Result := total = (b.account1.balance + b.account2.balance)
		end

	tb3: BOOLEAN
		local
			b: BANK
			m1, m2, m3, m4: MONEY
			ra: MONEY_ARRAY
		do
			comment ("tb3: test bank features: deposit, withdraw and transfer for 15%% of the balance")
			create b.make
			create m1.make_with_float (create {FLOAT}.make_from_real (601.02))
			create m2.make_with_float (create {FLOAT}.make_from_integer_32 (500))
			b.account1.deposit (m1)
			b.account2.deposit (m2)
			Result := b.account1.balance = m1 and b.account2.balance = m2
			check
					Result
			end
			ra := m1.allocated_by_ratios (create {NUM_ARRAY [INTEGER_32]}.from_array (<<15, 85>>))
			check
					ra [1] = create {MONEY}.make_with_float (create {FLOAT}.make_from_real (90.16))
			end
			b.account1.transfer (15, b.account2)
			create m3.make_with_float (create {FLOAT}.make_from_real (510.86))
			create m4.make_with_float (create {FLOAT}.make_from_real (590.16))
			Result := b.account1.balance = m3 and b.account2.balance = m4
			check
					Result
			end
		end
	
end -- class TEST_FLOAT1

Generated by ISE EiffelStudio