/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6827009 7071246
* @summary Positive tests for strings in switch.
* @author Joseph D. Darcy
*/
public class StringSwitches {
int failures = 0;
failures += testPileup();
failures += testSwitchingTwoWays();
failures += testNamedBreak();
failures += testExtraParens();
if (failures > 0) {
throw new RuntimeException();
}
}
/*
* A zero length string and all strings consisting only of the
* zero character \u0000 have a hash code of zero. This method
* maps such strings to the number of times \u0000 appears for 0
* through 6 occurrences.
*/
switch(s) {
case "":
return 0;
case "\u0000":
result = 1; break;
case "\u0000\u0000":
return 2;
case "\u0000\u0000\u0000":
result = 3; break;
case "\u0000\u0000\u0000\u0000":
return 4;
case "\u0000\u0000\u0000\u0000\u0000":
result = 5; break;
case "\u0000\u0000\u0000\u0000\u0000\u0000":
return 6;
default:
result = -1;
}
return result;
}
private static int testPileup() {
int failures = 0;
if (result != i) {
failures++;
}
}
failures++;
}
return failures;
}
/**
* Verify that a switch on an enum and a switch with the same
* structure on the string name of an enum compute equivalent
* values.
*/
private static int testSwitchingTwoWays() {
int failures = 0;
if (enumResult != stringResult) {
failures++;
"and 0x%x with the string one.%n",
}
}
return failures;
}
private static enum MetaSynVar {
FOO,
BAR,
BAZ,
QUX,
QUUX,
}
int result = 0;
switch(msv) {
case FOO:
// fallthrough:
case BAR:
case BAZ:
break;
default:
switch(msv) {
case QUX:
break;
case QUUX:
default:
}
break;
case MUMBLE:
return result;
case FOOBAR:
break;
}
return result;
}
int result = 0;
switch(msvName) {
case "FOO":
// fallthrough:
case "BAR":
case "BAZ":
break;
default:
switch(msvName) {
case "QUX":
break;
case "QUUX":
default:
}
break;
case "MUMBLE":
return result;
case "FOOBAR":
break;
}
return result;
}
private static int testNamedBreak() {
int failures = 0;
int expected = testExpected[i];
failures++;
}
}
return failures;
}
int result = 0;
outer: switch(s) {
case "a":
case "b":
case "c":
inner: switch(s + s) {
case "aa":
break inner;
case "cc":
break outer;
default:
return result;
}
case "d":
break outer;
default:
}
return result;
}
private static int testExtraParens() {
int failures = 1;
String s = "first";
switch(s) {
case (("first")):
failures = 0;
break;
case ("second"):
throw new RuntimeException("Should not be reached.");
}
return failures;
}
}