< Erlang (programming language) | TutorialsRevision as of 07:07, 8 August 2009 by imported>Tom Morris
The metadata subpage is missing. You can start it via filling in this form or by following the instructions that come up after clicking on the [show] link to the right.
|
Do you see this on PREVIEW? then SAVE! before following a link.
A - For a New Cluster use the following directions
Subpages format requires a metadata page.
Using the following instructions will complete the process of creating this article's subpages.
- Click the blue "metadata template" link below to create the page.
- On the edit page that appears paste in the article's title across from "
pagename = ".
- You might also fill out the checklist part of the form. Ignore the rest.
- For background, see Using the Subpages template Don't worry--you'll get the hang of it right away.
- Remember to hit Save!
the "metadata template".
However, you can create articles without subpages. Just delete the {{subpages}} template from the top of this page and this prompt will disappear. :) Don't feel obligated to use subpages, it's more important that you write sentences, which you can always do without writing fancy code.
|
B - For a Cluster Move use the following directions
The metadata template should be moved to the new name as the first step. Please revert this move and start by using the Move Cluster link at the top left of the talk page.
The name prior to this move can be found at the following link.
|
|
Sample program that uses eunit to test a function.
-module(power).
-export([pow/2, start/0]).
-include_lib("../../lib/eunit/include/eunit.hrl").
% adjust your own path
pow(_,0) -> 1;
pow(X,1) -> X;
pow(X,N) when N>1, is_integer(N) ->
X * pow(X,N-1).
start() ->
io:format(" This is a demo for eunit, the unit testing module.\n"),
io:format(" Now testing the pow() function with eunit\n"),
io:format(" Running: eunit:test(pow)~n"),
eunit:test(pow).
power_test_() ->
[?_assert(pow(0,0) == 1),
?_assert(pow(1,1) == 1),
?_assert(pow(2,2) == 4),
?_assert(pow(3,3) == 27),
?_assert(pow(4,4) == 256),
?_assertException(error, function_clause, pow(-1,0.5))
].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Note: Default unit test functions are automatically exported by eunit
%
% To test function power() we compile and test.
%
% 2> c(power).
% 3> eunit:test(pow).
% All 6 tests successful.
% But how does it work?
% eunit:test(pow) uses power:power_test_() to produce
% a list of 6 test functions(a test suite):
% 7> power:pow_test_().
% {15,#Fun<pow.1.106864721>},
% {16,#Fun<pow.2.81990874>},
% {17,#Fun<pow.3.122231603>},
% {18,#Fun<pow.4.90957845>},
% {19,#Fun<pow.5.90843223>},
% {20,#Fun<pow.4.90457845>}]
%
% eunit:test then runs the programs in the test suite
% and reports the results.
%
% Note: The numbers [15,16,17,18,19,20] are
% the line numbers of the source code that generated the
% test functions.
%
% Assertions
%
% Six total tests are run. An assertion is a positive assertion
% and expects the answer to be true. There are 5 positive assertions
% and 1 negative assertion in the test suite.
%
% Negative assertions
%
% A negative assertion is where an error is expected.
% If an error is generated, then the test succeeds.
% Since this power function does not know how to do
% negative bases, then it causes an error. Since an
% error is expected, the test succeeds.
%
% assertException(error, function_clause, pow(-1,0.5))
%
% is an example of a negative assertion.
% An error generates an exception. An exception is
% is a type of high level error message.
%
% See Exceptions
% for more details.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Unit tests as internal documentation
Unit tests provide a useful method of internal documentation that is
often easy to maintain. The maintenance of documentation can often
be a problem as programs evolve over time.
As long as the unit test works, the unit test serves as a nice suite
of example programs that can aid comprehension. For the purpose of
internal documentation and other reasons at least one
negative assertion is included to form a complete test suite.
A negative assertion can show the limits of
the domain or the range of a function, or even its time complexity
if one includes a timeout bound on computation time.
Software engineering
Unit tests can help one keep an eye on side-effects during
program development. Fixing a bug for some input may cause a
bug with some previously working input. Unit testing can help
with the organization of a program and inspire new solutions.
Note: for the program to work you will need to install
and perhaps compile the eunit module, and set up your path
to include it.