Created
March 18, 2014 22:13
-
-
Save willh/9630923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static org.junit.Assert.fail; | |
public class StringComparisonTest { | |
@Test | |
public void testCompileTimeVariable() { | |
String testCompileTimeString = "compileTime"; | |
if ("compileTime" == testCompileTimeString) { | |
System.out.print("equal"); | |
} else { | |
fail("unequal"); | |
} | |
} | |
@Test | |
public void testLiteralValue() { | |
if ("literal" == "literal") { | |
System.out.print("equal"); | |
} else { | |
fail("unequal"); | |
} | |
} | |
@Test | |
public void testStatementVariable() { | |
// defined as "statement" at compile time | |
String resultOfConcatenation = "state" + "ment"; | |
if ("statement" == resultOfConcatenation) { | |
System.out.print("equal"); | |
} else { | |
fail("unequal"); | |
} | |
} | |
@Test | |
public void testArrayItemVariable() { | |
String[] stringArray = {"hello", "world"}; | |
if ("hello" == stringArray[0]) { | |
System.out.print("equal"); | |
} else { | |
fail("unequal"); | |
} | |
} | |
@Test | |
public void testNewStringAtRuntime() { | |
String newString = new String("newString"); | |
if ("newString" == newString) { | |
System.out.print("equal"); | |
} else { | |
fail("unequal"); | |
} | |
} | |
@Test | |
public void testCharArrayToStringAtRuntimeVariable() { | |
char[] charArray = {'h', 'e', 'l', 'l', 'o'}; | |
String charSequence = ""; | |
for (int i = 0; i < charArray.length; i++) { | |
// only becomes "hello" at runtime | |
charSequence += charArray[i]; | |
} | |
if ("hello" == charSequence) { | |
System.out.print("equal"); | |
} else { | |
fail("unequal"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment