Here is the implementation of a function absolute/1 which simply computes the absolute value of a number using pattern matching similarly to the Erlang BIF erlang:abs/1:
1 2 3 4 5 6 | |
It is very simple to understand: the pattern matching processor scans each clause of absolute/1 in the order they are declared, when a clause match then the pattern matching processor selects it and immediately executes the corresponding path of the function.
Now we want to check if the function absolute/1 works as expected by writing a unit test using the EUnit framework included in the Erlang/OTP distribution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Then we invoke num_test:test() in the Erlang runtime system:
$ erl Erlang R16A (erts-5.10) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] Eshell V5.10 (abort with ^G) 1> num_test:test(). All 8 tests passed. ok
All tests passed successfully.