aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test
diff options
context:
space:
mode:
authorGravatar tomlu <tomlu@google.com>2018-06-08 10:45:04 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-06-08 10:46:34 -0700
commitad34b9a7f3c1b9332eb93a80b0f4bc4499b1d0fa (patch)
tree2b935e4f69cb11f477271a2f42a920879cb5388e /src/test
parentcd751ca2bb3677a15e0187f764415a6659417624 (diff)
Use unsafe String operations when writing parameter files.
When a LATIN-1 parameter file is requested, we can take advantage of the fact that JDK9 strings are (usually) stored as LATIN-1. For UTF-8, we can still optimize for the common case where a LATIN-1 string contains only ASCII characters, as these are bit-identical between UTF-8 and LATIN-1. This would still be expected to be the vast majority of parameter file contents. RELNOTES: None PiperOrigin-RevId: 199816430
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/google/devtools/build/lib/BUILD1
-rw-r--r--src/test/java/com/google/devtools/build/lib/exec/ParameterFileTest.java38
-rw-r--r--src/test/java/com/google/devtools/build/lib/unsafe/BUILD18
-rw-r--r--src/test/java/com/google/devtools/build/lib/unsafe/StringUnsafeTest.java61
4 files changed, 118 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD
index 15b05fa809..c20948ac1b 100644
--- a/src/test/java/com/google/devtools/build/lib/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/BUILD
@@ -60,6 +60,7 @@ filegroup(
"//src/test/java/com/google/devtools/build/lib/skylark:srcs",
"//src/test/java/com/google/devtools/build/lib/skylarkdebug/server:srcs",
"//src/test/java/com/google/devtools/build/lib/skylarkinterface/processor:srcs",
+ "//src/test/java/com/google/devtools/build/lib/unsafe:srcs",
],
visibility = ["//src:__pkg__"],
)
diff --git a/src/test/java/com/google/devtools/build/lib/exec/ParameterFileTest.java b/src/test/java/com/google/devtools/build/lib/exec/ParameterFileTest.java
index 92ea767ca1..4f49092cdf 100644
--- a/src/test/java/com/google/devtools/build/lib/exec/ParameterFileTest.java
+++ b/src/test/java/com/google/devtools/build/lib/exec/ParameterFileTest.java
@@ -16,11 +16,16 @@ package com.google.devtools.build.lib.exec;
import static com.google.common.truth.Truth.assertThat;
+import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.ParameterFile;
+import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType;
import com.google.devtools.build.lib.testutil.FoundationTestCase;
import com.google.devtools.build.lib.testutil.Suite;
import com.google.devtools.build.lib.testutil.TestSpec;
import com.google.devtools.build.lib.vfs.PathFragment;
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -39,4 +44,37 @@ public class ParameterFileTest extends FoundationTestCase {
assertThat(ParameterFile.derivePath(PathFragment.create("b")))
.isEqualTo(PathFragment.create("b-2.params"));
}
+
+ @Test
+ public void testWriteAscii() throws Exception {
+ assertThat(writeContent(StandardCharsets.ISO_8859_1, ImmutableList.of("--foo", "--bar")))
+ .containsExactly("--foo", "--bar");
+ assertThat(writeContent(StandardCharsets.UTF_8, ImmutableList.of("--foo", "--bar")))
+ .containsExactly("--foo", "--bar");
+ }
+
+ @Test
+ public void testWriteLatin1() throws Exception {
+ assertThat(writeContent(StandardCharsets.ISO_8859_1, ImmutableList.of("--füü")))
+ .containsExactly("--füü");
+ assertThat(writeContent(StandardCharsets.UTF_8, ImmutableList.of("--füü")))
+ .containsExactly("--füü");
+ }
+
+ @Test
+ public void testWriteUtf8() throws Exception {
+ assertThat(writeContent(StandardCharsets.ISO_8859_1, ImmutableList.of("--lambda=λ")))
+ .containsExactly("--lambda=?");
+ assertThat(writeContent(StandardCharsets.UTF_8, ImmutableList.of("--lambda=λ")))
+ .containsExactly("--lambda=λ");
+ }
+
+ private static ImmutableList<String> writeContent(Charset charset, Iterable<String> content)
+ throws Exception {
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ ParameterFile.writeParameterFile(outputStream, content, ParameterFileType.UNQUOTED, charset);
+ return ImmutableList.<String>builder()
+ .add(new String(outputStream.toByteArray(), charset).split("\n"))
+ .build();
+ }
}
diff --git a/src/test/java/com/google/devtools/build/lib/unsafe/BUILD b/src/test/java/com/google/devtools/build/lib/unsafe/BUILD
new file mode 100644
index 0000000000..3220a80b2c
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/unsafe/BUILD
@@ -0,0 +1,18 @@
+package(
+ default_visibility = ["//src:__subpackages__"],
+)
+
+filegroup(
+ name = "srcs",
+ srcs = glob(["**"]),
+)
+
+java_test(
+ name = "StringUnsafeTest",
+ srcs = ["StringUnsafeTest.java"],
+ deps = [
+ "//src/main/java/com/google/devtools/build/lib/unsafe:string",
+ "//third_party:junit4",
+ "//third_party:truth",
+ ],
+)
diff --git a/src/test/java/com/google/devtools/build/lib/unsafe/StringUnsafeTest.java b/src/test/java/com/google/devtools/build/lib/unsafe/StringUnsafeTest.java
new file mode 100644
index 0000000000..2801d521c3
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/unsafe/StringUnsafeTest.java
@@ -0,0 +1,61 @@
+// Copyright 2018 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.google.devtools.build.lib.unsafe;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link StringUnsafe}. */
+@RunWith(JUnit4.class)
+public class StringUnsafeTest {
+
+ @Test
+ public void testGetCoder() {
+ if (!StringUnsafe.canUse()) {
+ return;
+ }
+ StringUnsafe stringUnsafe = StringUnsafe.getInstance();
+ assertThat(stringUnsafe.getCoder("")).isEqualTo(StringUnsafe.LATIN1);
+ assertThat(stringUnsafe.getCoder("hello")).isEqualTo(StringUnsafe.LATIN1);
+ assertThat(stringUnsafe.getCoder("lambda λ")).isEqualTo(StringUnsafe.UTF16);
+ }
+
+ @Test
+ public void testGetBytes() {
+ if (!StringUnsafe.canUse()) {
+ return;
+ }
+ StringUnsafe stringUnsafe = StringUnsafe.getInstance();
+ assertThat(ByteBuffer.wrap(stringUnsafe.getByteArray("hello")))
+ .isEqualTo(StandardCharsets.ISO_8859_1.encode("hello"));
+ assertThat(ByteBuffer.wrap(stringUnsafe.getByteArray("lambda λ")))
+ .isEqualTo(StandardCharsets.UTF_16LE.encode("lambda λ"));
+ }
+
+ @Test
+ public void testNewInstance() throws Exception {
+ if (!StringUnsafe.canUse()) {
+ return;
+ }
+ StringUnsafe stringUnsafe = StringUnsafe.getInstance();
+ String s = "hello";
+ assertThat(stringUnsafe.newInstance(stringUnsafe.getByteArray(s), stringUnsafe.getCoder(s)))
+ .isEqualTo("hello");
+ }
+}