Skip to content

Commit

Permalink
Add tests for BigInt emitter (#4165)
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed May 28, 2024
1 parent 3ae8acd commit 6e5ce38
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/com/google/javascript/jscomp/CodeConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ void addNumber(double x, Node n) {
void addBigInt(BigInteger bi) {
String hexEncoded = "0x" + bi.toString(16) + "n";
String decimalEncoded = bi.toString() + "n";
// The exact threshold is when bi >= 10n ** 17n
addConstant(hexEncoded.length() < decimalEncoded.length() ? hexEncoded : decimalEncoded);
}

Expand Down
34 changes: 34 additions & 0 deletions test/com/google/javascript/jscomp/CodePrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.javascript.rhino.Token;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -47,6 +48,39 @@ public void testBigInt() {
assertPrint("-0b110n", "-6n");
assertPrint("-0o7n", "-7n");
assertPrint("-0x8n", "-8n");
assertPrintSame("1000000000n");
assertPrintSame("-10000000000n");

// Normalize the results to lowercase so the tests do not enfore the output is lowercase but
// instead the capitalization is left to the compiler.
Function<String, String> normalizeBigInt = s ->
s.substring(0, s.length() - 1).toLowerCase() + s.substring(s.length() - 1);
assertPrintAfterNormalization(
"7182582809266874004429817360811601465721521679690913024666851123813995272999n",
"0xfe132a356ec5f9279946c8fdf29780abd0387a8277e811069d174660b385b27n",
normalizeBigInt);
assertPrintAfterNormalization(
"0x000000fe132a356ec5f9279946c8fdf29780abd0387a8277e811069d174660b385b27n",
"0xfe132a356ec5f9279946c8fdf29780abd0387a8277e811069d174660b385b27n",
normalizeBigInt);
assertPrintAfterNormalization(
"-7182582809266874004429817360811601465721521679690913024666851123813995272999n",
"-0xfe132a356ec5f9279946c8fdf29780abd0387a8277e811069d174660b385b27n",
normalizeBigInt);
assertPrintAfterNormalization(
"-0o00774114521526730576223631215443757451360052750070365011677201040647213506301316055447n",
"-0xfe132a356ec5f9279946c8fdf29780abd0387a8277e811069d174660b385b27n",
normalizeBigInt);
assertPrintAfterNormalization(
"0o774114521526730576223631215443757451360052750070365011677201040647213506301316055447n",
"0xfe132a356ec5f9279946c8fdf29780abd0387a8277e811069d174660b385b27n",
normalizeBigInt);
assertPrintAfterNormalization(
"0b111111100001001100101010001101010110111011000101111110010010011110011001010001101100100" +
"0111111011111001010010111100000001010101111010000001110000111101010000010011101111110" +
"10000001000100000110100111010001011101000110011000001011001110000101101100100111n",
"0xfe132a356ec5f9279946c8fdf29780abd0387a8277e811069d174660b385b27n",
normalizeBigInt);
}

@Test
Expand Down
12 changes: 9 additions & 3 deletions test/com/google/javascript/jscomp/CodePrinterTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.javascript.jscomp.parsing.Config;
import com.google.javascript.rhino.Node;
import java.nio.charset.Charset;
import java.util.function.Function;
import org.jspecify.nullness.Nullable;
import org.junit.Before;

Expand Down Expand Up @@ -166,10 +167,11 @@ void assertPrettyPrintNode(String expectedJs, Node ast) {
assertThat(prettyPrintNode(ast)).isEqualTo(expectedJs);
}

protected void assertPrint(String js, String expected) {
protected void assertPrintAfterNormalization(String js, String expected,
Function<String, String> normalize) {
parse(expected); // validate the expected string is valid JS
assertThat(
parsePrint(
normalize.apply(parsePrint(
js,
newCompilerOptions(
new CompilerOptionBuilder() {
Expand All @@ -179,10 +181,14 @@ void setOptions(CompilerOptions options) {
options.setLineLengthThreshold(
CompilerOptions.DEFAULT_LINE_LENGTH_THRESHOLD);
}
})))
}))))
.isEqualTo(expected);
}

protected void assertPrint(String js, String expected) {
assertPrintAfterNormalization(js, expected, Function.identity());
}

protected void assertPrintSame(String js) {
assertPrint(js, js);
}
Expand Down

0 comments on commit 6e5ce38

Please sign in to comment.