aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/test/erl_match_SUITE_data/match_test.c
blob: 153a528b0bfecd6ce264a1ad9552ef7f51d22d1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1997-2009. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */

/*
 * Purpose: Tests the erl_match() function.
 * Author:  Bjorn Gustavsson
 */

#include "runner.h"

TESTCASE(erl_match_server)
{
    erl_init(NULL, 0);

    for (;;) {
	ETERM* pattern;
	ETERM* term;

	pattern = get_term();
	if (pattern == NULL) {
	    report(1);
	    return;
	} else {
	    term = get_term();
	    if (term == NULL) {
		fail("Unexpected EOF term");
	    } else {
		send_term(erl_mk_int(erl_match(pattern, term)));
		erl_free_term(pattern);
		erl_free_term(term);
	    }
	}
    }

}

TESTCASE(erl_match_bind)
{
  erl_init(NULL, 0);

  for (;;) {
    char* pattern;
    ETERM* term;

    pattern=read_packet(NULL);

    switch (pattern[0]) {
    case 'e':
      free(pattern);
      report(1);
      return;

    case 'b':
      {
	ETERM* patt_term;

	/*
	 * Get the pattern string and convert it using erl_format().
	 *
	 * Note that the call to get_term() below destroys the buffer
	 * that the pattern variable points to.  Therefore, it is
	 * essential to call erl_format() here, before 
	 * calling get_term().
	 */

	message("Pattern: %s", pattern+1);
	patt_term = erl_format(pattern+1);
	free(pattern);

	if (patt_term == NULL) {
	  fail("erl_format() failed");
	}

	/*
	 * Get the term and send back the result of the erl_match()
	 * call.
	 */

	term = get_term();
	if (term == NULL) {
	  fail("Unexpected eof term");
	}
	else {
	  send_term(erl_mk_int(erl_match(patt_term, term)));
	}
	erl_free_term(patt_term);
	erl_free_term(term);
      }
    break;

    default:
      free(pattern);
      fail("Illegal character received");
    }
      
  }
}