aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/benchmark/javatests
diff options
context:
space:
mode:
authorGravatar Yue Gan <yueg@google.com>2017-03-17 13:49:25 +0000
committerGravatar Yue Gan <yueg@google.com>2017-03-20 11:32:42 +0000
commit45c731083ae620e8c56f75ab2503b261788f4720 (patch)
tree42949ae194e7db6ab8a2e36f65f539a86d4f6add /src/tools/benchmark/javatests
parentdd142c93fd4939aaec30726f275644ca9446fea2 (diff)
Add CppCodeGenerator for bencharmk without enabling it. It will be enabled in a following change.
-- PiperOrigin-RevId: 150434910 MOS_MIGRATED_REVID=150434910
Diffstat (limited to 'src/tools/benchmark/javatests')
-rw-r--r--src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/BUILD22
-rw-r--r--src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/CppCodeGeneratorHelperTest.java283
-rw-r--r--src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/CppCodeGeneratorTest.java106
-rw-r--r--src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/JavaCodeGeneratorTest.java3
4 files changed, 413 insertions, 1 deletions
diff --git a/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/BUILD b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/BUILD
index f97a5cc9df..dba4c5b39f 100644
--- a/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/BUILD
+++ b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/BUILD
@@ -22,6 +22,28 @@ java_test(
)
java_test(
+ name = "CppCodeGeneratorHelperTest",
+ srcs = ["CppCodeGeneratorHelperTest.java"],
+ deps = [
+ "//src/tools/benchmark/java/com/google/devtools/build/benchmark/codegenerator:codegenerator_lib",
+ "//third_party:guava",
+ "//third_party:junit4",
+ "//third_party:truth",
+ ],
+)
+
+java_test(
+ name = "CppCodeGeneratorTest",
+ srcs = ["CppCodeGeneratorTest.java"],
+ deps = [
+ "//src/tools/benchmark/java/com/google/devtools/build/benchmark/codegenerator:codegenerator_lib",
+ "//third_party:guava",
+ "//third_party:junit4",
+ "//third_party:truth",
+ ],
+)
+
+java_test(
name = "MainTest",
srcs = ["MainTest.java"],
deps = [
diff --git a/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/CppCodeGeneratorHelperTest.java b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/CppCodeGeneratorHelperTest.java
new file mode 100644
index 0000000000..aea46ff88c
--- /dev/null
+++ b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/CppCodeGeneratorHelperTest.java
@@ -0,0 +1,283 @@
+// Copyright 2017 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.benchmark.codegenerator;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.base.Joiner;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.Scanner;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Test for {@link CppCodeGeneratorHelper}. */
+@RunWith(JUnit4.class)
+public class CppCodeGeneratorHelperTest {
+
+ private static final String RANDOM_CLASS_HEADER_CONTENT = joinLines(
+ "class ClassName {",
+ "public:",
+ " static void printSth();",
+ "};");
+
+ private static final String RANDOM_CLASS_CONTENT = joinLines(
+ "#include <random>",
+ "#include <iostream>",
+ "#include <ctime>",
+ "using namespace std;",
+ "",
+ "class ClassName {",
+ "public:",
+ " static void printSth() {",
+ " srand(time(NULL));",
+ " int n = rand();",
+ " cout << \"This is method(printSth) with random number(\" << n << \")\\n\";",
+ " }",
+ "};");
+
+ private static final String RANDOM_CLASS_HEADER_EXTRA_CONTENT = joinLines(
+ "class ClassName {",
+ "public:",
+ " static void printSth();",
+ " static void printSthElse();",
+ "};");
+
+ private static final String RANDOM_CLASS_EXTRA_CONTENT = joinLines(
+ "#include <random>",
+ "#include <iostream>",
+ "#include <ctime>",
+ "using namespace std;",
+ "",
+ "class ClassName {",
+ "public:",
+ " static void printSth() {",
+ " srand(time(NULL));",
+ " int n = rand();",
+ " cout << \"This is method(printSth) with random number(\" << n << \")\\n\";",
+ " }",
+ " static void printSthElse() {",
+ " srand(time(NULL));",
+ " int n = rand();",
+ " cout << \"This is method(printSthElse) with random number(\" << n << \")\\n\";",
+ " }",
+ "};");
+
+ private static final String ALL_FILES_BUILD_FILE_CONTENT = joinLines(
+ "cc_library(",
+ " name = 'target',",
+ " srcs = glob([ '*.cc', '*.h' ]),",
+ ")");
+
+ private static final String RANDOM_CLASS_HEADER_NEXT_CONTENT = joinLines(
+ "class Deps42 {",
+ "public:",
+ " static void printSth();",
+ " static void callNext();",
+ "};");
+
+ private static final String RANDOM_CLASS_NEXT_CONTENT = joinLines(
+ "#include <random>",
+ "#include <iostream>",
+ "#include <ctime>",
+ "#include \"Deps43.h\"",
+ "using namespace std;",
+ "",
+ "class Deps42 {",
+ "public:",
+ " static void printSth() {",
+ " srand(time(NULL));",
+ " int n = rand();",
+ " cout << \"This is method(printSth) with random number(\" << n << \")\\n\";",
+ " }",
+ " static void callNext() {",
+ " Deps43::printSth();",
+ " }",
+ "};");
+
+ private static final String BUILD_FILE_NEXT_CONTENT = joinLines(
+ "cc_library(",
+ " name = 'Deps42',",
+ " srcs = [ 'Deps42.cc', 'Deps42.h' ],",
+ " deps = [ ':Deps43' ],",
+ ")");
+
+ private static final String RANDOM_CLASS_HEADER_NEXT_EXTRA_CONTENT = joinLines(
+ "class Deps42 {",
+ "public:",
+ " static void printSth();",
+ " static void printSthElse();",
+ " static void callNext();",
+ "};");
+
+ private static final String RANDOM_CLASS_NEXT_EXTRA_CONTENT = joinLines(
+ "#include <random>",
+ "#include <iostream>",
+ "#include <ctime>",
+ "#include \"Deps43.h\"",
+ "using namespace std;",
+ "",
+ "class Deps42 {",
+ "public:",
+ " static void printSth() {",
+ " srand(time(NULL));",
+ " int n = rand();",
+ " cout << \"This is method(printSth) with random number(\" << n << \")\\n\";",
+ " }",
+ " static void printSthElse() {",
+ " srand(time(NULL));",
+ " int n = rand();",
+ " cout << \"This is method(printSthElse) with random number(\" << n << \")\\n\";",
+ " }",
+ " static void callNext() {",
+ " Deps43::printSth();",
+ " }",
+ "};");
+
+ private static final String BUILD_FILE_CONTENT = joinLines(
+ "cc_library(",
+ " name = 'target',",
+ " srcs = [ 'target.cc', 'target.h' ],",
+ ")");
+
+ private static final String MAIN_CLASS_CONTENT = joinLines(
+ "int main() {",
+ " return 0;",
+ "}");
+
+ private static final String MAIN_CLASS_BUILD_FILE_CONTENT = joinLines(
+ "cc_binary(",
+ " name = 'target',",
+ " srcs = [ 'Main.cc' ],",
+ "This is deps",
+ ")");
+
+ @Rule
+ public TemporaryFolder folder = new TemporaryFolder();
+
+ @Test
+ public void testCreateRandomClass() throws IOException {
+ Path dir = folder.newFolder("CreateRandomClass").toPath();
+ CppCodeGeneratorHelper.createRandomClass("ClassName", dir);
+
+ Path cppFile = dir.resolve("ClassName.h");
+ assertThat(cppFile.toFile().exists()).isTrue();
+ String content = new Scanner(cppFile).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(RANDOM_CLASS_HEADER_CONTENT);
+
+ cppFile = dir.resolve("ClassName.cc");
+ assertThat(cppFile.toFile().exists()).isTrue();
+ content = new Scanner(cppFile).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(RANDOM_CLASS_CONTENT);
+ }
+
+ @Test
+ public void testCreateRandomClassExtra() throws IOException {
+ Path dir = folder.newFolder("CreateRandomClassExtra").toPath();
+ CppCodeGeneratorHelper.createRandomClassExtra("ClassName", dir);
+
+ Path cppFile = dir.resolve("ClassName.h");
+ assertThat(cppFile.toFile().exists()).isTrue();
+ String content = new Scanner(cppFile).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(RANDOM_CLASS_HEADER_EXTRA_CONTENT);
+
+ cppFile = dir.resolve("ClassName.cc");
+ assertThat(cppFile.toFile().exists()).isTrue();
+ content = new Scanner(cppFile).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(RANDOM_CLASS_EXTRA_CONTENT);
+ }
+
+ @Test
+ public void testWriteBuildFileWithAllFilesToDir() throws IOException {
+ Path dir = folder.newFolder("WriteBuildFileWithAllFilesToDir").toPath();
+ CppCodeGeneratorHelper.writeBuildFileWithAllFilesToDir("target", dir);
+
+ Path buildFile = dir.resolve("BUILD");
+ assertThat(buildFile.toFile().exists()).isTrue();
+ String content = new Scanner(buildFile).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(ALL_FILES_BUILD_FILE_CONTENT);
+ }
+
+ @Test
+ public void testCreateClassAndBuildFileWithDepsNext() throws IOException {
+ Path dir = folder.newFolder("CreateClassAndBuildFileWithDepsNext").toPath();
+ CppCodeGeneratorHelper.createClassAndBuildFileWithDepsNext(42, dir);
+
+ Path file = dir.resolve("Deps42.h");
+ assertThat(file.toFile().exists()).isTrue();
+ String content = new Scanner(file).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(RANDOM_CLASS_HEADER_NEXT_CONTENT);
+
+ file = dir.resolve("Deps42.cc");
+ assertThat(file.toFile().exists()).isTrue();
+ content = new Scanner(file).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(RANDOM_CLASS_NEXT_CONTENT);
+
+ file = dir.resolve("BUILD");
+ assertThat(file.toFile().exists()).isTrue();
+ content = new Scanner(file).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(BUILD_FILE_NEXT_CONTENT);
+ }
+
+ @Test
+ public void testCreateClassWithDepsNextExtra() throws IOException {
+ Path dir = folder.newFolder("CreateClassWithDepsNextExtra").toPath();
+ CppCodeGeneratorHelper.createClassWithDepsNextExtra(42, dir);
+
+ Path file = dir.resolve("Deps42.h");
+ assertThat(file.toFile().exists()).isTrue();
+ String content = new Scanner(file).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(RANDOM_CLASS_HEADER_NEXT_EXTRA_CONTENT);
+
+ file = dir.resolve("Deps42.cc");
+ assertThat(file.toFile().exists()).isTrue();
+ content = new Scanner(file).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(RANDOM_CLASS_NEXT_EXTRA_CONTENT);
+ }
+
+ @Test
+ public void testAppendTargetToBuildFile() throws IOException {
+ Path dir = folder.newFolder("AppendTargetToBuildFile").toPath();
+ CppCodeGeneratorHelper.appendTargetToBuildFile("target", dir);
+
+ Path file = dir.resolve("BUILD");
+ assertThat(file.toFile().exists()).isTrue();
+ String content = new Scanner(file).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(BUILD_FILE_CONTENT);
+ }
+
+ @Test
+ public void testCreateMainClassAndBuildFileWithDeps() throws IOException {
+ Path dir = folder.newFolder("CreateMainClassAndBuildFileWithDeps").toPath();
+ CppCodeGeneratorHelper.createMainClassAndBuildFileWithDeps("target", "This is deps", dir);
+
+ Path file = dir.resolve("Main.cc");
+ assertThat(file.toFile().exists()).isTrue();
+ String content = new Scanner(file).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(MAIN_CLASS_CONTENT);
+
+ file = dir.resolve("BUILD");
+ assertThat(file.toFile().exists()).isTrue();
+ content = new Scanner(file).useDelimiter("\\Z").next();
+ assertThat(content).isEqualTo(MAIN_CLASS_BUILD_FILE_CONTENT);
+ }
+
+ private static String joinLines(String... lines) {
+ return Joiner.on("\n").join(lines);
+ }
+}
diff --git a/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/CppCodeGeneratorTest.java b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/CppCodeGeneratorTest.java
new file mode 100644
index 0000000000..68d2dc27ee
--- /dev/null
+++ b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/CppCodeGeneratorTest.java
@@ -0,0 +1,106 @@
+// Copyright 2017 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.benchmark.codegenerator;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.collect.ImmutableSet;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Test for {@link CppCodeGenerator}. */
+@RunWith(JUnit4.class)
+public class CppCodeGeneratorTest {
+
+ @Rule public TemporaryFolder folder = new TemporaryFolder();
+
+ @Test
+ public void testGenerateNewProject() throws IOException {
+ File createdFolder = folder.newFolder("GenerateNewProject");
+ Path dir = createdFolder.toPath();
+ (new CppCodeGenerator()).generateNewProject(dir.toString(), true, true, true, true);
+
+ // Check dir contains 4 project directories
+ File[] filesList = dir.toFile().listFiles();
+ assertThat(filesList).isNotNull();
+ ImmutableSet<String> filenames = fileArrayToImmutableSet(filesList);
+ assertThat(filenames).containsExactly(
+ CppCodeGenerator.TARGET_A_FEW_FILES,
+ CppCodeGenerator.TARGET_LONG_CHAINED_DEPS,
+ CppCodeGenerator.TARGET_MANY_FILES,
+ CppCodeGenerator.TARGET_PARALLEL_DEPS);
+
+ // Target 1: a few files
+ checkSimpleTarget(
+ dir, JavaCodeGenerator.TARGET_A_FEW_FILES, JavaCodeGenerator.SIZE_A_FEW_FILES);
+
+ // Target 2: many files
+ checkSimpleTarget(dir, JavaCodeGenerator.TARGET_MANY_FILES, JavaCodeGenerator.SIZE_MANY_FILES);
+
+ // Target 3: long chained deps
+ checkDepsTarget(
+ dir, JavaCodeGenerator.TARGET_LONG_CHAINED_DEPS, JavaCodeGenerator.SIZE_LONG_CHAINED_DEPS);
+
+ // Target 4: parallel deps
+ checkDepsTarget(
+ dir, JavaCodeGenerator.TARGET_PARALLEL_DEPS, JavaCodeGenerator.SIZE_PARALLEL_DEPS);
+ }
+
+ private static ImmutableSet<String> fileArrayToImmutableSet(File[] files) {
+ ImmutableSet.Builder<String> builder = ImmutableSet.builder();
+ for (File file : files) {
+ builder.add(file.getName());
+ }
+ return builder.build();
+ }
+
+ private static void checkSimpleTarget(Path root, String targetName, int targetSize) {
+ // Check all files including BUILD, .cc, .h
+ File[] filesList =
+ root.resolve(targetName).toFile().listFiles();
+ assertThat(filesList).isNotNull();
+ ImmutableSet<String> filenames = fileArrayToImmutableSet(filesList);
+ ImmutableSet.Builder<String> randomClassNames = ImmutableSet.builder();
+ randomClassNames.add("BUILD");
+ for (int i = 0; i < targetSize; ++i) {
+ randomClassNames.add("RandomClass" + i + ".h");
+ randomClassNames.add("RandomClass" + i + ".cc");
+ }
+ assertThat(filenames).containsExactlyElementsIn(randomClassNames.build());
+ }
+
+ private static void checkDepsTarget(Path root, String targetName, int targetSize) {
+ // Check all files including BUILD, .cc, .h
+ File[] filesList =
+ root.resolve(targetName).toFile().listFiles();
+ assertThat(filesList).isNotNull();
+ ImmutableSet<String> filenames = fileArrayToImmutableSet(filesList);
+ ImmutableSet.Builder<String> randomClassNames = ImmutableSet.builder();
+ randomClassNames.add("BUILD");
+ randomClassNames.add("Main.cc");
+ for (int i = 1; i < targetSize; ++i) {
+ randomClassNames.add("Deps" + i + ".h");
+ randomClassNames.add("Deps" + i + ".cc");
+ }
+ assertThat(filenames).containsExactlyElementsIn(randomClassNames.build());
+ }
+} \ No newline at end of file
diff --git a/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/JavaCodeGeneratorTest.java b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/JavaCodeGeneratorTest.java
index f135b1fb77..016e82e065 100644
--- a/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/JavaCodeGeneratorTest.java
+++ b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/codegenerator/JavaCodeGeneratorTest.java
@@ -38,7 +38,8 @@ public class JavaCodeGeneratorTest {
public void testGenerateNewProject() throws IOException {
File createdFolder = folder.newFolder("GenerateNewProject");
Path dir = createdFolder.toPath();
- JavaCodeGenerator.generateNewProject(dir.toString(), true, true, true, true);
+ JavaCodeGenerator javaCodeGenerator = new JavaCodeGenerator();
+ javaCodeGenerator.generateNewProject(dir.toString(), true, true, true, true);
// Check dir contains 4 project directories
File[] filesList = dir.toFile().listFiles();