regex_OK.ttcn 9.98 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
balaskoa's avatar
balaskoa committed
2
 * Copyright (c) 2000-2020 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Raduly, Csaba
 *
Elemer Lelik's avatar
Elemer Lelik committed
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
 ******************************************************************************/
module regex_OK {	//^In TTCN-3 module `regex_OK'://

type component PDTestComponent {};


/*--- REGEXP_CHARSTRING --------------------------------------------------*/


testcase regexp_char() runs on PDTestComponent{ //In testcase definition//

//var charstring reg_char1 := "123abcd _efg\nhhhhx"

// POSITIVE TEST CASES
var charstring reg[55];

reg[0] 	:= regexp("123hhh"		,"[1-3]#(,)([h-h]#(,))"		,0)
reg[1] 	:= regexp("123hhh"		,"[1-3]#(3,)(h#(3,))"		,0)
reg[2] 	:= regexp("123hhh"		,"[1-3]#(3,4)(h#(3,4))"		,0)
reg[3] 	:= regexp("123hhh"		,"[1-3]#(1,3)(h#(1,3))"		,0)
reg[4] 	:= regexp("123hhh"		,"[1-3]#(,3)(h#(,3))"		,0)

reg[5] 	:= regexp("333 hhh"		,"[^1-2]#(,) ([^a-g,i-z]#(,))" ,0)
reg[6] 	:= regexp("333 hhh"		,"[^1-2,4]#(,) ([^a-g,i]#(,))" ,0)
reg[7] 	:= regexp("333 hhh"		,"[^1,2,4]#(,) ([^a,b,i]#(,))" ,0) //In variable assignment//   //In operation `regexp\(\)'//   //In character string pattern//   //Charstring pattern: Duplicate character `.' in the character set//2
reg[8] 	:= regexp("333 hhh"		,"[^1-2]#(,)[3] ([^a-g]#(,))"  ,0)
reg[9]  := regexp("333 hhh"		,"[^1-2]#(,)[33] ([^i-z]#(,))" ,0) //In variable assignment//   //In operation `regexp\(\)'//   //In character string pattern//   //Charstring pattern: Duplicate character `.' in the character set//

reg[10]  := regexp("333 hhh"		,"[^1-2]#(,)[3,33] ([h,g]#(,))" ,0) //In variable assignment//   //In operation `regexp\(\)'//   //In character string pattern//   //Charstring pattern: Duplicate character `.' in the character set//2
reg[11]  := regexp("333 hhh"		,"[^1-2]#(,)[33,3,4] ([h]#(,))" ,0) //In variable assignment//   //In operation `regexp\(\)'//   //In character string pattern//   //Charstring pattern: Duplicate character `.' in the character set//3
reg[12]  := regexp("333 hhh"		,"[^1-2]#(,) ([h,hh]h#(,))"     ,0) //In variable assignment//   //In operation `regexp\(\)'//   //In character string pattern//   //Charstring pattern: Duplicate character `.' in the character set//2
reg[13]  := regexp("333 hhh"		,"[^1-2]#(,) ([h,hh,hhh]#(,))"  ,0) //In variable assignment//   //In operation `regexp\(\)'//   //In character string pattern//   //Charstring pattern: Duplicate character `.' in the character set//6
reg[14]  := regexp("333 hhh"		,"[^1-2]#(,) ([h,g,hh]#(,))"    ,0) //In variable assignment//   //In operation `regexp\(\)'//   //In character string pattern//   //Charstring pattern: Duplicate character `.' in the character set//3

reg[15]  := regexp("333 hhh "		,"\d\d\d (\w\w\w) "    ,0) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//6
reg[16]  := regexp("333 hhh "		,"\w#(,) (\w\w#(,2)) " ,0) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//3
reg[17]  := regexp("333 hhh "		,"\w#(,) (\w#(,2)\w) " ,0) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//3
reg[18]  := regexp("333 hhh "		,"\w#(3,) (\w?\w) "    ,0) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//3
reg[19]  := regexp("333 hhh "		,"\w\d3 (h\wh) "       ,0) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//3

reg[20]  := regexp("333\thhh "		,"????(???)?"          ,0)
reg[21]  := regexp("333\thhh "		,"3??\t(?h?)?"         ,0)
reg[22]  := regexp("333\thhh "		,"?#(,)\t(?#(,))?"     ,0)
reg[23]  := regexp("333\thhh "		,"?#(,)?(h?\w) "       ,0) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//
reg[24]  := regexp("333\thhh "		,"?#(,3)\t(h??#(,2))?" ,0)

reg[25]  := regexp("333\rhhh "		,"?*\r(*) "	     ,0)
reg[26]  := regexp("333\rhhh "		,"*?3\r(h*) "    ,0)
reg[27]  := regexp("333\rhhh "		,"*333\r(***h) " ,0)
reg[28]  := regexp("333\rhhh "		,"*(h*?h)?"      ,0)
reg[29]  := regexp("333\rhhh "		,"*?(???)?"      ,0)

reg[30]  := regexp("333\rhhh "		,"*???\r(?*h#(2,)) " ,0)
reg[31]  := regexp("333\rhhh "		,"\w\d*\r(\w?*) "    ,0) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//3
reg[32]  := regexp("333\rhhh "		,"*?(h*h\w) "        ,0) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//
reg[33]  := regexp("333\rhhh "		,"*\r(\w\w\w) "      ,0) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//3
reg[34]  := regexp("333\rhhh "		,"*\r(*) "           ,0)

reg[35]  := regexp("333\"hhh "		,"*\"(???)(?)*"    ,0)
reg[36]  := regexp("333\"hhh "		,"*3?(?*??)[ ,1]"  ,0)
reg[37]  := regexp("333\"hhh "		,"*3?(???)[1, ,2]" ,0) //In variable assignment//   //In operation `regexp\(\)'//   //In character string pattern//   //Charstring pattern: Duplicate character `.' in the character set//
Elemer Lelik's avatar
Elemer Lelik committed
74
75
reg[38]  := regexp @nocase ("333\"hhh "		,"*33?(???)[^a,1]" ,0)
reg[39]  := regexp @nocase ("333\"hhh "		,"333\"(???)*"     ,0)
Elemer Lelik's avatar
Elemer Lelik committed
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155


for (var integer j:=0; j<=39; j:=j+1) {
	if (reg[j] == "hhh")
	     {setverdict(pass);}
	else {setverdict(fail);}
}


reg[40] 	:= regexp("123\rabc"	,"(*)123(*)abc"	,1)
reg[41] 	:= regexp("\r123"	,"(*)123(*)"	,0)
reg[42] 	:= regexp("\r123\r"	,"(*)123(?)"	,1)
reg[43] 	:= regexp("\t\n\r123\r"	,"??(?)123(?)"	,0)
reg[44] 	:= regexp("\t\n\r123"	,"(?)(?)(?)(*)"	,2)

reg[45] 	:= regexp("(\r)"	,"(\()(?)(\))"	,1) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//2
reg[46] 	:= regexp("\"(\r)\""	,"?(\()(?)(\))?",1) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//2
reg[47] 	:= regexp("\r"		,"(?)"		,0)
reg[48] 	:= regexp("\r"		,"(*)"		,0)
reg[49] 	:= regexp("\r"		,"(**)"		,0)

reg[50] 	:= regexp("\r \t \n"	,"(*) (*) (*)"	,0)
reg[51] 	:= regexp("\r\r\r\r"	,"(*)(\r#(3))"	,0)
reg[52] 	:= regexp("\r"		,"([^\n,\t])"	,0)
reg[53] 	:= regexp("\r "		,"(*)(?)(?)"	,1)
reg[54] 	:= regexp(" \r "	,"(?)(*?*) "	,1)


for (var integer j:=40; j<=54; j:=j+1) {
	if (reg[j] == "\r")
	     {setverdict(pass);}
	else {setverdict(fail);}
}


// NEGATIVE TEST CASES
var charstring regn[10];

regn[0] 	:= regexp("\r"		,"(?)(?)"	,1)
regn[1] 	:= regexp("\r \n"	,"(?)(\n)"	,1)
regn[2] 	:= regexp("12ab"	,"(\d\d\d)(\w)"	,1) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//4
regn[3] 	:= regexp("12ab"	,"(\d#(3))(?)"	,1) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//
regn[4] 	:= regexp("\r"		,"(?)(r*)"	,1)

regn[5] 	:= regexp("\r"		,"(*)([\t,\n])"	,1)
regn[6] 	:= regexp("\r"		,"(*)([^\r])"	,1)
regn[7] 	:= regexp("123 abc"	,"123??(a??)"	,0)
regn[8] 	:= regexp("1234"	,"([1-3]#(,))"	,0)
regn[9] 	:= regexp("123a"	,"(\d#(,))"	,0) //In charstring value//   //Unknown escape sequence `\\.' was treated literally//

for (var integer j:=0; j<=9; j:=j+1) {
	if (regn[j] == "")
	     {setverdict(pass);}
	else {setverdict(fail);}
}

}

testcase  regexp_degen() runs on PDTestComponent {
  var charstring reg[6];
  //These would be errors:
  //reg[-2] := regexp(strng, "([]+)", 0);
  //reg[-1] := regexp(strng, "([^]+)", 0);
  reg[0] := regexp("@@@]]]###", "*([]]#3)(*)", 0); // a right square bracket
  reg[1] := regexp("]]]!!!]]]", "*([^]]#3)(*)", 0); // not a right square bracket

  reg[2] := regexp("+++---!!!", "*([-]#3)(*)", 0); // a dash
  reg[3] := regexp("---???---", "*([^-]#3)(*)", 0); // not a dash

  reg[4] := regexp("[[[-]-+++", "*([]-]#3)(*)", 0); // a right square bracket or dash
  reg[5] := regexp("[[[---]]]", "*([^]-]#3)(*)", 0);// not a right square bracket or dash

  for(var integer i:=0; i<6; i:=i+1) {
    log(reg[i]);
    if( lengthof(reg[i])==3 )
	     {setverdict(pass);}
	else {setverdict(fail);}
  }
}

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const charstring c_cs_val := "aBc5XYz";
const charstring c_cs_pattern := "(abc?xyz)";

// case-insensitive regexp
testcase regexp_nocase() runs on PDTestComponent {
  // regexp with constants (calculated at compile-time)
  var charstring v_res_const := regexp @nocase (c_cs_val, c_cs_pattern, 0);
  if (v_res_const != c_cs_val) {
    setverdict(fail, "Const regexp failed. Got: ", v_res_const, ", expected: ", c_cs_val);
  }
  
  // regexp with variables (calculated at runtime)
  var charstring v_cs_val := "aBE5XYz";
  var charstring v_cs_pattern := "(ab[A-e]?xyz)";
  var charstring v_res_var := regexp @nocase (v_cs_val, v_cs_pattern, 0);
  if (v_res_var != v_cs_val) {
    setverdict(fail, "Var regexp failed. Got: ", v_res_var, ", expected: ", v_cs_val);
  }
  setverdict(pass);
}

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
testcase Bug_565852() runs on PDTestComponent {
  // issue #1: Titan does not support the notation '#()'
  var charstring cs_input := "xxxxaaaxxxx";
  var charstring cs_pattern := "x#()(a#(1,))x#()";
  var charstring cs_result := regexp(cs_input, cs_pattern, 0);
  var charstring cs_expected := "aaa";
  if (cs_result != cs_expected) {
    setverdict(fail, "Test #1 failed. Expected: ", cs_expected, ", got: ", cs_result);
  }
  
  // issue #2: '[^a]' and 'a#(...)' do not work for unversal charstrings
  var universal charstring us_input := "xxxxaaaxxxx";
  var universal charstring us_pattern := "*[^a](a#(1,))[^a]*";
  var universal charstring us_result := regexp(us_input, us_pattern, 0);
  var universal charstring us_expected := "aaa";
  if (us_result != us_expected) {
    setverdict(fail, "Test #2 failed. Expected: ", us_expected, ", got: ", us_result);
  }
  
  // extra issue: regexp(<charstring>, <universal charstring>, ...) generates incorrect code
  var universal charstring mixed_result := regexp(cs_input, us_pattern, 0);
  var universal charstring mixed_expected := "aaa";
  if (mixed_result != mixed_expected) {
    setverdict(fail, "Test #3 failed. Expected: ", mixed_expected, ", got: ", mixed_result);
  }
  setverdict(pass);
}

Elemer Lelik's avatar
Elemer Lelik committed
205
206
207
control {
	execute (regexp_char());
	execute (regexp_degen());
208
	execute (regexp_nocase());
209
	execute (Bug_565852());
Elemer Lelik's avatar
Elemer Lelik committed
210
211
212
}

}