diff options
author | Ulf Adams <ulfjack@google.com> | 2015-08-07 09:58:58 +0000 |
---|---|---|
committer | Dmitry Lomov <dslomov@google.com> | 2015-08-10 10:09:12 +0000 |
commit | d2ae11fb4524e9fe8897c43f5aa2307b5ea531e7 (patch) | |
tree | afd54ce8aeb6b7333f711f12fb2ff71ca5c1c6a7 | |
parent | 518b24624987c7f3bb8b7339cdcedcd0756567b0 (diff) |
Externalize file_test and strings_test, and fix up the BUILD files.
--
MOS_MIGRATED_REVID=100109450
-rw-r--r-- | src/main/cpp/util/BUILD | 14 | ||||
-rw-r--r-- | src/test/cpp/util/BUILD | 34 | ||||
-rw-r--r-- | src/test/cpp/util/file_test.cc | 39 | ||||
-rw-r--r-- | src/test/cpp/util/md5_test.cc | 52 | ||||
-rw-r--r-- | src/test/cpp/util/strings_test.cc | 295 |
5 files changed, 428 insertions, 6 deletions
diff --git a/src/main/cpp/util/BUILD b/src/main/cpp/util/BUILD index ef3b2f952f..76438c13de 100644 --- a/src/main/cpp/util/BUILD +++ b/src/main/cpp/util/BUILD @@ -31,15 +31,17 @@ cc_library( cc_library( name = "strings", srcs = ["strings.cc"], - hdrs = [ - # This really belongs into its own library, but that doesn't work on the - # Mac, because of issue #61 (header-only libraries don't build). - "exit_code.h", - "strings.h", - ], + hdrs = ["strings.h"], # Automatically propagate the symbol definition to rules depending on this. defines = [ "BLAZE_OPENSOURCE", ], visibility = ["//visibility:public"], + deps = [":blaze_exit_code"], +) + +cc_library( + name = "blaze_exit_code", + hdrs = ["exit_code.h"], + visibility = ["//visibility:public"], ) diff --git a/src/test/cpp/util/BUILD b/src/test/cpp/util/BUILD new file mode 100644 index 0000000000..1c62d7d289 --- /dev/null +++ b/src/test/cpp/util/BUILD @@ -0,0 +1,34 @@ +# Description: +# C++ utility tests for Bazel + +cc_test( + name = "md5_test", + srcs = ["md5_test.cc"], + deps = [ + "//src/main/cpp/util", + "//src/main/cpp/util:md5", + "//third_party:gtest", + ], +) + +cc_test( + name = "file_test", + srcs = ["file_test.cc"], + deps = [ + "//src/main/cpp/util", + "//third_party:gtest", + ], +) + +cc_test( + name = "strings_test", + srcs = ["strings_test.cc"], + deps = [ + "//src/main/cpp/util:strings", + "//third_party:gtest", + ], +) + +test_suite( + name = "all_tests", +) diff --git a/src/test/cpp/util/file_test.cc b/src/test/cpp/util/file_test.cc new file mode 100644 index 0000000000..eb81165c9c --- /dev/null +++ b/src/test/cpp/util/file_test.cc @@ -0,0 +1,39 @@ +// Copyright 2014 Google Inc. 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. +#include "src/main/cpp/util/file.h" +#include "gtest/gtest.h" + +namespace blaze_util { + +TEST(BlazeUtil, JoinPath) { + string path = JoinPath("", ""); + ASSERT_EQ("", path); + + path = JoinPath("a", "b"); + ASSERT_EQ("a/b", path); + + path = JoinPath("a/", "b"); + ASSERT_EQ("a/b", path); + + path = JoinPath("a", "/b"); + ASSERT_EQ("a/b", path); + + path = JoinPath("a/", "/b"); + ASSERT_EQ("a/b", path); + + path = JoinPath("/", "/"); + ASSERT_EQ("/", path); +} + +} // namespace blaze_util diff --git a/src/test/cpp/util/md5_test.cc b/src/test/cpp/util/md5_test.cc new file mode 100644 index 0000000000..991d8c5ccd --- /dev/null +++ b/src/test/cpp/util/md5_test.cc @@ -0,0 +1,52 @@ +// Copyright 2014 Google Inc. 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. +#include "src/main/cpp/util/md5.h" +#include "src/main/cpp/util/port.h" +#include "gtest/gtest.h" + +namespace blaze_util { + +TEST(BlazeUtil, Basic) { + const char *strs[] = { + "", + "a", + "abc", + "message digest", + "abcdefghijklmnopqrstuvwxyz", + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "12345678901234567890123456789012345678901234567890123456789012345678901234567890" + }; + const char *md5s[] = { + "d41d8cd98f00b204e9800998ecf8427e", + "0cc175b9c0f1b6a831c399e269772661", + "900150983cd24fb0d6963f7d28e17f72", + "f96b697d7cb7938d525a2f31aaf161d0", + "c3fcd3d76192e4007dfb496cca67e13b", + "d174ab98d277d9f5a5611c2c9f419d9f", + "57edf4a22be3c955ac49da2e2107b67a", + }; + uint n = arraysize(strs); + ASSERT_EQ(n, arraysize(md5s)); + + unsigned char buf[17]; + Md5Digest digest; + for (uint i = 0; i < n; i++) { + digest.Reset(); + digest.Update(strs[i], strlen(strs[i])); + digest.Finish(buf); + ASSERT_EQ(md5s[i], digest.String()); + } +} + +} // namespace blaze_util diff --git a/src/test/cpp/util/strings_test.cc b/src/test/cpp/util/strings_test.cc new file mode 100644 index 0000000000..1a7c203838 --- /dev/null +++ b/src/test/cpp/util/strings_test.cc @@ -0,0 +1,295 @@ +// Copyright 2014 Google Inc. 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. +#include "src/main/cpp/util/strings.h" +#include "gtest/gtest.h" + +namespace blaze_util { + +TEST(BlazeUtil, JoinStrings) { + vector<string> pieces; + string output; + JoinStrings(pieces, ' ', &output); + ASSERT_EQ("", output); + + pieces.push_back("abc"); + JoinStrings(pieces, ' ', &output); + ASSERT_EQ("abc", output); + + pieces.push_back(""); + JoinStrings(pieces, ' ', &output); + ASSERT_EQ("abc ", output); + + pieces.push_back("def"); + JoinStrings(pieces, ' ', &output); + ASSERT_EQ("abc def", output); +} + +TEST(BlazeUtil, Split) { + string lines = ""; + vector<string> pieces = Split(lines, '\n'); + ASSERT_EQ(0, pieces.size()); + + lines = "foo"; + pieces = Split(lines, '\n'); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + + lines = "\nfoo"; + pieces = Split(lines, '\n'); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + + lines = "\n\n\nfoo"; + pieces = Split(lines, '\n'); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + + lines = "foo\n"; + pieces = Split(lines, '\n'); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + + lines = "foo\n\n\n"; + pieces = Split(lines, '\n'); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + + lines = "foo\nbar"; + pieces = Split(lines, '\n'); + ASSERT_EQ(2, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + ASSERT_EQ("bar", pieces[1]); + + lines = "foo\n\nbar"; + pieces = Split(lines, '\n'); + ASSERT_EQ(2, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + ASSERT_EQ("bar", pieces[1]); +} + +TEST(BlazeUtil, Replace) { + string line = "foo\\\nbar\nbaz"; + Replace("\\\n", "", &line); + ASSERT_EQ("foobar\nbaz", line); + + line = "foo\\\n\\\nbar"; + Replace("\\\n", "", &line); + ASSERT_EQ("foobar", line); + + line = "foo\\\r\nbar"; + Replace("\\\r\n", "", &line); + ASSERT_EQ("foobar", line); + + line = "\\\n\\\r\n"; + Replace("\\\n", "", &line); + Replace("\\\r\n", "", &line); + ASSERT_EQ("", line); + + line = "x:y:z"; + Replace(":", "_C", &line); + ASSERT_EQ("x_Cy_Cz", line); + + line = "x_::y_:__z"; + Replace("_", "_U", &line); + Replace(":", "_C", &line); + ASSERT_EQ("x_U_C_Cy_U_C_U_Uz", line); +} + +TEST(BlazeUtil, StripWhitespace) { + string str = " "; + StripWhitespace(&str); + ASSERT_EQ("", str); + + str = " abc "; + StripWhitespace(&str); + ASSERT_EQ("abc", str); + + str = "abc"; + StripWhitespace(&str); + ASSERT_EQ("abc", str); +} + +TEST(BlazeUtil, Tokenize) { + vector<string> result; + string str = "a b c"; + Tokenize(str, '#', &result); + ASSERT_EQ(3, result.size()); + EXPECT_EQ("a", result[0]); + EXPECT_EQ("b", result[1]); + EXPECT_EQ("c", result[2]); + + str = "a 'b c'"; + Tokenize(str, '#', &result); + ASSERT_EQ(2, result.size()); + EXPECT_EQ("a", result[0]); + EXPECT_EQ("b c", result[1]); + + str = "foo# bar baz"; + Tokenize(str, '#', &result); + ASSERT_EQ(1, result.size()); + EXPECT_EQ("foo", result[0]); + + str = "foo # bar baz"; + Tokenize(str, '#', &result); + ASSERT_EQ(1, result.size()); + EXPECT_EQ("foo", result[0]); + + str = "#bar baz"; + Tokenize(str, '#', &result); + ASSERT_EQ(0, result.size()); + + str = "#"; + Tokenize(str, '#', &result); + ASSERT_EQ(0, result.size()); + + str = " \tfirst second / "; + Tokenize(str, 0, &result); + ASSERT_EQ(3, result.size()); + EXPECT_EQ("first", result[0]); + EXPECT_EQ("second", result[1]); + EXPECT_EQ("/", result[2]); + + str = " \tfirst second / "; + Tokenize(str, '/', &result); + ASSERT_EQ(2, result.size()); + EXPECT_EQ("first", result[0]); + EXPECT_EQ("second", result[1]); + + str = "first \"second' third\" fourth"; + Tokenize(str, '/', &result); + ASSERT_EQ(3, result.size()); + EXPECT_EQ("first", result[0]); + EXPECT_EQ("second' third", result[1]); + EXPECT_EQ("fourth", result[2]); + + str = "first 'second\" third' fourth"; + Tokenize(str, '/', &result); + ASSERT_EQ(3, result.size()); + EXPECT_EQ("first", result[0]); + EXPECT_EQ("second\" third", result[1]); + EXPECT_EQ("fourth", result[2]); + + str = "\\ this\\ is\\ one\\'\\ token"; + Tokenize(str, 0, &result); + ASSERT_EQ(1, result.size()); + EXPECT_EQ(" this is one' token", result[0]); + + str = "\\ this\\ is\\ one\\'\\ token\\"; + Tokenize(str, 0, &result); + ASSERT_EQ(1, result.size()); + EXPECT_EQ(" this is one' token", result[0]); + + str = "unterminated \" runs to end of line"; + Tokenize(str, 0, &result); + ASSERT_EQ(2, result.size()); + EXPECT_EQ("unterminated", result[0]); + EXPECT_EQ(" runs to end of line", result[1]); + + str = ""; + Tokenize(str, 0, &result); + ASSERT_EQ(0, result.size()); + + str = "one two\'s three"; + Tokenize(str, 0, &result); + ASSERT_EQ(2, result.size()); + EXPECT_EQ("one", result[0]); + EXPECT_EQ("twos three", result[1]); + + str = "one \'two three"; + Tokenize(str, 0, &result); + ASSERT_EQ(2, result.size()); + EXPECT_EQ("one", result[0]); + EXPECT_EQ("two three", result[1]); +} + +static vector<string> SplitQuoted(const string &contents, + const char delimeter) { + vector<string> result; + SplitQuotedStringUsing(contents, delimeter, &result); + return result; +} + +TEST(BlazeUtil, SplitQuoted) { + string lines = ""; + vector<string> pieces = SplitQuoted(lines, '\n'); + ASSERT_EQ(0, pieces.size()); + + // Same behaviour without quotes as Split + lines = "foo"; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + + lines = " foo"; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + + lines = " foo"; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + + lines = "foo "; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + + lines = "foo "; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + + lines = "foo bar"; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(2, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + ASSERT_EQ("bar", pieces[1]); + + lines = "foo bar"; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(2, pieces.size()); + ASSERT_EQ("foo", pieces[0]); + ASSERT_EQ("bar", pieces[1]); + + // Test with quotes + lines = "' 'foo"; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("' 'foo", pieces[0]); + + lines = " ' ' foo"; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(2, pieces.size()); + ASSERT_EQ("' '", pieces[0]); + ASSERT_EQ("foo", pieces[1]); + + lines = "foo' \\' ' "; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo' \\' '", pieces[0]); + + lines = "foo'\\'\" ' "; + pieces = SplitQuoted(lines, ' '); + ASSERT_EQ(1, pieces.size()); + ASSERT_EQ("foo'\\'\" '", pieces[0]); +} + +TEST(BlazeUtil, StringPrintf) { + string out; + StringPrintf(&out, "%s %s", "a", "b"); + EXPECT_EQ("a b", out); +} + +} // namespace blaze_util |