Skip to content

Conversion functions from primitive types to strings #5048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
132 changes: 132 additions & 0 deletions jbmc/regression/jbmc-strings/CProverStringToString/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import org.cprover.CProverString;

class Main {
public void testIntSuccess(int choice) {
int i;
String r;

switch (choice) {
case 0:
i = 0;
r = "0";
break;
case 1:
i = 1;
r = "1";
break;
case 2:
i = -1;
r = "-1";
break;
case 3:
i = Integer.MIN_VALUE;
r = "-2147483648";
case 4:
i = Integer.MAX_VALUE;
r = "2147483647";
default:
return;
}

String s = CProverString.toString(i);
assert s.equals(r);
}

public void testIntFailure() {
String s = CProverString.toString(123);
assert s.equals("0");
}

public void testLongSuccess(int choice) {
long l;
String r;

switch (choice) {
case 0:
l = 0L;
r = "0";
break;
case 1:
l = 1L;
r = "1";
break;
case 2:
l = -1L;
r = "-1";
break;
case 3:
l = Long.MIN_VALUE;
;
r = "-9223372036854775808";
break;
case 4:
l = Long.MAX_VALUE;
r = "9223372036854775807";
break;
default:
return;
}

String s = CProverString.toString(l);
assert s.equals(r);
}

public void testLongFailure() {
String s = CProverString.toString(9223372036854775807L);
assert s.equals("0");
}

public void testFloatSuccess(int choice) {
float f;
String r;

switch (choice) {
case 0:
f = 0F;
r = "0.0";
break;
case 1:
f = 1F;
r = "1.0";
break;
case 2:
f = -1F;
r = "-1.0";
break;
case 3:
f = 1.1F;
r = "1.1";
break;
case 4:
f = Float.MAX_VALUE;
r = "3.4028235E38";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of a weird way of formatting this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍞

break;
case 5:
f = Float.MIN_VALUE;
r = "1.4E-45";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did this number come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the result of String.valueOf(f) (same as Float.toString(f)) 🍞

break;
case 6:
f = Float.POSITIVE_INFINITY;
r = "Infinity";
break;
case 7:
f = Float.NEGATIVE_INFINITY;
r = "-Infinity";
break;
case 8:
f = Float.NaN;
r = "NaN";
break;
default:
return;
}

String s = CProverString.toString(f);
assert s.equals(r);
}

public void testFloatFailure() {
String s = CProverString.toString(1.1F);
assert s.equals("0");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
Main.class
--function Main.testFloatFailure --cp `../../../../scripts/format_classpath.sh . ../../../lib/java-models-library/target/core-models.jar`
^VERIFICATION FAILED$
^EXIT=10$
^SIGNAL=0$
--
--
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
KNOWNBUG
Main.class
--function Main.testFloatSuccess --cp `../../../../scripts/format_classpath.sh . ../../../lib/java-models-library/target/core-models.jar`
^VERIFICATION SUCCESSFUL$
^EXIT=0$
^SIGNAL=0$
--
--
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
Main.class
--function Main.testIntFailure --cp `../../../../scripts/format_classpath.sh . ../../../lib/java-models-library/target/core-models.jar`
^VERIFICATION FAILED$
^EXIT=10$
^SIGNAL=0$
--
--
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
Main.class
--function Main.testIntSuccess --cp `../../../../scripts/format_classpath.sh . ../../../lib/java-models-library/target/core-models.jar`
^VERIFICATION SUCCESSFUL$
^EXIT=0$
^SIGNAL=0$
--
--
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
Main.class
--function Main.testLongFailure --cp `../../../../scripts/format_classpath.sh . ../../../lib/java-models-library/target/core-models.jar`
^VERIFICATION FAILED$
^EXIT=10$
^SIGNAL=0$
--
--
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
Main.class
--function Main.testLongSuccess --cp `../../../../scripts/format_classpath.sh . ../../../lib/java-models-library/target/core-models.jar`
^VERIFICATION SUCCESSFUL$
^EXIT=0$
^SIGNAL=0$
--
--
Binary file added jbmc/regression/jbmc-strings/CharToString/Test.class
Binary file not shown.
13 changes: 13 additions & 0 deletions jbmc/regression/jbmc-strings/CharToString/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Test {
public void test1() {
char c = '\uFFFF';
String s = String.valueOf(c);
assert s.equals("\uFFFF");
}

public void test2() {
char c = '\uFFFE';
String s = String.valueOf(c);
assert s.equals("\uFFFE");
}
}
6 changes: 6 additions & 0 deletions jbmc/regression/jbmc-strings/CharToString/test1.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KNOWNBUG
Test.class
--function Test.test1 --cp `../../../../scripts/format_classpath.sh . ../../../lib/java-models-library/target/core-models.jar`
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
6 changes: 6 additions & 0 deletions jbmc/regression/jbmc-strings/CharToString/test2.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CORE
Test.class
--function Test.test2 --cp `../../../../scripts/format_classpath.sh . ../../../lib/java-models-library/target/core-models.jar`
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
Binary file modified jbmc/regression/jbmc-strings/cprover/CProverString.class
Binary file not shown.
8 changes: 8 additions & 0 deletions jbmc/regression/jbmc-strings/cprover/CProverString.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,12 @@ public static StringBuffer insert(
StringBuffer sb, int offset, long l) {
return sb.insert(offset, l);
}

public static String toString(int i) { return String.valueOf(i); }

public static String toString(long l) { return String.valueOf(l); }

public static String toString(float f) { return String.valueOf(f); }

public static String toString(double d) { return String.valueOf((float)d); }
}
Loading