Skip to content

Commit

Permalink
Merge pull request #879 from BYVoid/bazel
Browse files Browse the repository at this point in the history
Merge Bazel build.
  • Loading branch information
BYVoid authored Jul 26, 2024
2 parents 38ef9d8 + f9f2829 commit 2303080
Show file tree
Hide file tree
Showing 32 changed files with 903 additions and 625 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: C++ CI with Bazel

on:
push:
branches: [ master ]
branches: [master, bazel]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:
Expand All @@ -13,4 +13,4 @@ jobs:
- uses: actions/checkout@v4
- uses: bazelbuild/setup-bazelisk@v3
- run: bazel build //:opencc
- run: bazel test //src/...
- run: bazel test --test_output=all //src/... //data/... //test/...
13 changes: 13 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "opencc",
hdrs = [
"//src:Export.hpp",
"//src:SimpleConverter.hpp",
"//src:opencc.h",
],
data = [
"//data/config",
"//data/dictionary:binary_dictionaries",
"//data/dictionary:text_dictionaries",
],
strip_include_prefix = "src",
deps = [
"//src:opencc",
],
Expand Down
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ module(
bazel_dep(name = "darts-clone", version = "0.32")
bazel_dep(name = "googletest", version = "1.14.0.bcr.1")
bazel_dep(name = "marisa-trie", version = "0.2.6")
bazel_dep(name = "rapidjson", version = "1.1.0")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_python", version = "0.34.0")
bazel_dep(name = "tclap", version = "1.2.5")
487 changes: 25 additions & 462 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions data/config/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "config",
srcs = glob(["*.json"]),
)
79 changes: 79 additions & 0 deletions data/dictionary/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package(default_visibility = ["//visibility:public"])

genrule(
name = "merge_TWPhrases",
srcs = [
"TWPhrasesIT.txt",
"TWPhrasesName.txt",
"TWPhrasesOther.txt",
],
outs = ["TWPhrases.txt"],
cmd = "$(location //data/scripts:merge) " +
"$(SRCS) $(OUTS)",
tools = ["//data/scripts:merge"],
)

[
genrule(
name = "reverse_" + txt,
srcs = [txt + ".txt"],
outs = [txt + "Rev.txt"],
cmd = "$(location //data/scripts:reverse) " +
"$(SRCS) $(OUTS)",
tools = ["//data/scripts:reverse"],
)
for txt in [
"TWVariants",
"TWPhrases",
"HKVariants",
"JPVariants",
]
]

TEXT_DICTS = glob(["*.txt"]) + [
"TWPhrases.txt",
"TWVariantsRev.txt",
"TWPhrasesRev.txt",
"HKVariantsRev.txt",
"JPVariantsRev.txt",
]

[
genrule(
name = "generate_bin_" + txt[:-4],
srcs = [txt],
outs = [txt.replace(".txt", ".ocd2")],
cmd = "$(location //src/tools:dict_converter) " +
"--input $(location " + txt + ") " +
"--output $(OUTS) " +
"--from text " +
"--to ocd2",
tools = ["//src/tools:dict_converter"],
)
for txt in TEXT_DICTS
]

filegroup(
name = "text_dictionaries",
srcs = TEXT_DICTS,
)

filegroup(
name = "binary_dictionaries",
srcs = [txt.replace(".txt", ".ocd2") for txt in TEXT_DICTS],
)

cc_test(
name = "dictionary_test",
srcs = ["DictionaryTest.cpp"],
data = [
":binary_dictionaries",
":text_dictionaries",
],
deps = [
"//src:lexicon",
"//src:marisa_dict",
"//src:utf8_util",
"@googletest//:gtest_main",
],
)
90 changes: 90 additions & 0 deletions data/dictionary/DictionaryTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Open Chinese Convert
*
* Copyright 2024-2024 Carbo Kuo <byvoid@byvoid.com>
*
* 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 "gtest/gtest.h"

#include "src/Lexicon.hpp"
#include "src/MarisaDict.hpp"
#include "src/UTF8Util.hpp"

namespace opencc {

const char* RUNFILE_SUFFIX = ".runfiles/_main";

class DictionaryTest : public ::testing::Test,
public ::testing::WithParamInterface<std::string> {
protected:
static void SetUpTestSuite() {

std::string program_filename = ::testing::internal::GetArgvs().front();
size_t suffix_pos = program_filename.find(RUNFILE_SUFFIX);
ASSERT_NE(suffix_pos, std::string::npos);

runfile_dir_ =
program_filename.substr(0, suffix_pos + strlen(RUNFILE_SUFFIX));
}

static std::string runfile_dir_;
};

std::string DictionaryTest::runfile_dir_;

INSTANTIATE_TEST_SUITE_P(
, DictionaryTest,
::testing::Values("HKVariants", "HKVariantsRevPhrases",
"JPShinjitaiCharacters", "JPShinjitaiPhrases",
"JPVariants", "STCharacters", "STPhrases", "TSCharacters",
"TSPhrases", "TWPhrasesIT", "TWPhrasesName",
"TWPhrasesOther", "TWVariants", "TWVariantsRevPhrases",
"TWPhrases", "TWVariantsRev", "TWPhrasesRev",
"HKVariantsRev", "JPVariantsRev"),
[](const testing::TestParamInfo<DictionaryTest::ParamType>& info) {
return info.param;
});

TEST_P(DictionaryTest, UniqueSortedTest) {
const std::string dictionaryFileName =
runfile_dir_ + "/data/dictionary/" + GetParam() + ".txt";
FILE* fp =
fopen(UTF8Util::GetPlatformString(dictionaryFileName).c_str(), "rb");
ASSERT_NE(fp, nullptr);
LexiconPtr lexicon = Lexicon::ParseLexiconFromFile(fp);
EXPECT_TRUE(lexicon->IsUnique()) << GetParam() << " has duplicated keys.";
EXPECT_TRUE(lexicon->IsSorted()) << GetParam() << " is not sorted.";
}

TEST_P(DictionaryTest, BinaryTest) {
const std::string binaryDictionaryFileName =
runfile_dir_ + "/data/dictionary/" + GetParam() + ".ocd2";
FILE* fp_bin = fopen(
UTF8Util::GetPlatformString(binaryDictionaryFileName).c_str(), "rb");
ASSERT_NE(fp_bin, nullptr);
MarisaDictPtr dict = MarisaDict::NewFromFile(fp_bin);
ASSERT_NE(dict, nullptr);

const std::string textDictionaryFileName =
runfile_dir_ + "/data/dictionary/" + GetParam() + ".txt";
FILE* fp_txt =
fopen(UTF8Util::GetPlatformString(textDictionaryFileName).c_str(), "rb");
ASSERT_NE(fp_txt, nullptr);
LexiconPtr txt_lexicon = Lexicon::ParseLexiconFromFile(fp_txt);

EXPECT_EQ(dict->GetLexicon()->Length(), txt_lexicon->Length());
}

} // namespace opencc
Loading

0 comments on commit 2303080

Please sign in to comment.