Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / RegExp / regress-57631.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is mozilla.org code.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   pschwartau@netscape.com, zack-weg@gmx.de
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 /*
40  * Date: 26 November 2000
41  *
42  *
43  * SUMMARY:  This test arose from Bugzilla bug 57631:
44  * "RegExp with invalid pattern or invalid flag causes segfault"
45  *
46  * Either error should throw an exception of type SyntaxError,
47  * and we check to see that it does...
48  */
49 //-----------------------------------------------------------------------------
50 var BUGNUMBER = '57631';
51 var summary = 'Testing new RegExp(pattern,flag) with illegal pattern or flag';
52 var statprefix = 'Testing for error creating illegal RegExp object on pattern ';
53 var statsuffix =  'and flag ';
54 var cnSUCCESS = 'SyntaxError';
55 var cnFAILURE = 'not a SyntaxError';
56 var singlequote = "'";
57 var i = -1; var j = -1; var s = ''; var f = '';
58 var obj = {};
59 var status = ''; var actual = ''; var expect = ''; var msg = '';
60 var legalpatterns = new Array(); var illegalpatterns = new Array();
61 var legalflags = new Array();  var illegalflags = new Array();
62
63
64 // valid regular expressions to try -
65 legalpatterns[0] = '';
66 legalpatterns[1] = 'abc';
67 legalpatterns[2] = '(.*)(3-1)\s\w';
68 legalpatterns[3] = '(.*)(...)\\s\\w';
69 legalpatterns[4] = '[^A-Za-z0-9_]';
70 legalpatterns[5] = '[^\f\n\r\t\v](123.5)([4 - 8]$)';
71
72 // invalid regular expressions to try -
73 illegalpatterns[0] = '(?)';
74 illegalpatterns[1] = '(a';
75 illegalpatterns[2] = '( ]';
76 //illegalpatterns[3] = '\d{1,s}';
77
78 // valid flags to try -
79 legalflags[0] = 'i';
80 legalflags[1] = 'g';
81 legalflags[2] = 'm';
82 legalflags[3] = undefined;
83
84 // invalid flags to try -
85 illegalflags[0] = 'a';
86 illegalflags[1] = 123;
87 illegalflags[2] = new RegExp();
88
89
90
91 //-------------------------------------------------------------------------------------------------
92 test();
93 //-------------------------------------------------------------------------------------------------
94
95
96 function test()
97 {
98   enterFunc ('test');
99   printBugNumber(BUGNUMBER);
100   printStatus (summary);
101  
102   testIllegalRegExps(legalpatterns, illegalflags);
103   testIllegalRegExps(illegalpatterns, legalflags);
104   testIllegalRegExps(illegalpatterns, illegalflags);
105
106   exitFunc ('test');
107 }
108
109
110 // This function will only be called where all the patterns are illegal, or all the flags
111 function testIllegalRegExps(patterns, flags)
112 {
113   for (i in patterns)
114   {
115     s = patterns[i];
116  
117     for (j in flags)
118     {
119       f = flags[j];
120       status = getStatus(s, f);
121       actual = cnFAILURE;
122       expect = cnSUCCESS;
123  
124       try
125       {
126         // This should cause an exception if either s or f is illegal -        
127         eval('obj = new RegExp(s, f);'); 
128       } 
129       catch(e)
130       {
131         // We expect to get a SyntaxError - test for this:
132         if (e instanceof SyntaxError)
133           actual = cnSUCCESS;
134       }
135        
136       reportCompare(expect, actual, status);
137     }
138   }
139 }
140
141
142 function getStatus(regexp, flag)
143 {
144   return (statprefix  +  quote(regexp) +  statsuffix  +   quote(flag));
145 }
146
147
148 function quote(text)
149 {
150   return (singlequote  +  text  + singlequote);
151 }