4
%% Copyright Ericsson AB 1997-2010. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
23
-export([test/1, test/2,
24
start/1, send_term/2, finish/1, send_eot/1, recv_eot/1,
25
get_term/1, get_term/2]).
27
-define(default_timeout, test_server:seconds(5)).
29
%% Executes a test case in a C program.
31
%% This function is useful for test cases written in C which requires
32
%% no further input, and only returns a result by calling report().
35
test(Tc, ?default_timeout).
40
case get_term(Port, Timeout) of
44
io:format("In this test case, a success/failure result was"),
45
io:format("expected from the C program.\n"),
46
io:format("Received: ~p", [Other]),
50
%% Executes a test case in a C program. Returns the port.
54
%% Returns: {ok, Port}
56
start({Prog, Tc}) when is_list(Prog), is_integer(Tc) ->
57
Port = open_port({spawn, Prog}, [{packet, 4}]),
58
Command = [Tc div 256, Tc rem 256],
59
Port ! {self(), {command, Command}},
62
%% Finishes a test case by send an 'eot' message to the C program
63
%% and waiting for an 'eot'.
65
%% If the C program doesn't require an 'eot', use recv_eot/1 instead.
67
finish(Port) when is_port(Port) ->
71
%% Sends an Erlang term to a C program.
73
send_term(Port, Term) when is_port(Port) ->
74
Port ! {self(), {command, [$t, term_to_binary(Term)]}}.
76
%% Sends an 'eot' (end-of-test) indication to a C progrm.
78
send_eot(Port) when is_port(Port) ->
79
Port ! {self(), {command, [$e]}}.
81
%% Waits for an 'eot' indication from the C program.
82
%% Either returns 'ok' or invokes test_server:fail().
84
recv_eot(Port) when is_port(Port) ->
85
case get_term(Port) of
89
io:format("Error finishing test case. Expected eof from"),
90
io:format("C program, but got:"),
91
io:format("~p", [Other]),
95
%% Reads a term from the C program.
97
%% Returns: {term, Term}|eot|'NULL' or calls test_server:fail/1,2.
100
get_term(Port, ?default_timeout).
102
get_term(Port, Timeout) ->
103
case get_reply(Port, Timeout) of
109
test_server:fail(Reason);
111
{term, binary_to_term(list_to_binary(Term))};
117
io:format("~s", [Message]),
118
get_term(Port, Timeout);
120
io:format("Garbage received from C program: ~p", [Other]),
121
test_server:fail("Illegal response from C program")
124
get_reply(Port, Timeout) when is_port(Port) ->
126
{Port, {data, Reply}} ->
129
test_server:fail("No response from C program")