-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for C layer for the input types of Text Classifier
PiperOrigin-RevId: 569553038
- Loading branch information
1 parent
6915a79
commit 96fa10b
Showing
10 changed files
with
168 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
mediapipe/tasks/c/components/processors/classifier_options_converter_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* Copyright 2023 The MediaPipe Authors. | ||
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 "mediapipe/tasks/c/components/processors/classifier_options_converter.h" | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "mediapipe/framework/port/gtest.h" | ||
#include "mediapipe/tasks/c/components/processors/classifier_options.h" | ||
#include "mediapipe/tasks/cc/components/processors/classifier_options.h" | ||
|
||
namespace mediapipe::tasks::c::components::processors { | ||
|
||
constexpr char kCategoryAllowlist[] = "fruit"; | ||
constexpr char kCategoryDenylist[] = "veggies"; | ||
constexpr char kDisplayNamesLocaleGerman[] = "de"; | ||
|
||
TEST(ClassifierOptionsConverterTest, ConvertsClassifierOptionsCustomValues) { | ||
std::vector<const char*> category_allowlist = {kCategoryAllowlist}; | ||
std::vector<const char*> category_denylist = {kCategoryDenylist}; | ||
|
||
ClassifierOptions c_classifier_options = { | ||
/* display_names_locale= */ kDisplayNamesLocaleGerman, | ||
/* max_results= */ 1, | ||
/* score_threshold= */ 0.1, | ||
/* category_allowlist= */ category_allowlist.data(), | ||
/* category_allowlist_count= */ 1, | ||
/* category_denylist= */ category_denylist.data(), | ||
/* category_denylist_count= */ 1}; | ||
|
||
mediapipe::tasks::components::processors::ClassifierOptions | ||
cpp_classifier_options = {}; | ||
|
||
CppConvertToClassifierOptions(c_classifier_options, &cpp_classifier_options); | ||
EXPECT_EQ(cpp_classifier_options.display_names_locale, "de"); | ||
EXPECT_EQ(cpp_classifier_options.max_results, 1); | ||
EXPECT_FLOAT_EQ(cpp_classifier_options.score_threshold, 0.1); | ||
EXPECT_EQ(cpp_classifier_options.category_allowlist, | ||
std::vector<std::string>{"fruit"}); | ||
EXPECT_EQ(cpp_classifier_options.category_denylist, | ||
std::vector<std::string>{"veggies"}); | ||
} | ||
|
||
TEST(ClassifierOptionsConverterTest, ConvertsClassifierOptionsDefaultValues) { | ||
std::vector<const char*> category_allowlist = {kCategoryAllowlist}; | ||
std::vector<const char*> category_denylist = {kCategoryDenylist}; | ||
|
||
ClassifierOptions c_classifier_options = {/* display_names_locale= */ nullptr, | ||
/* max_results= */ -1, | ||
/* score_threshold= */ 0.0, | ||
/* category_allowlist= */ nullptr, | ||
/* category_allowlist_count= */ 0, | ||
/* category_denylist= */ nullptr, | ||
/* category_denylist_count= */ 0}; | ||
|
||
mediapipe::tasks::components::processors::ClassifierOptions | ||
cpp_classifier_options = {}; | ||
|
||
CppConvertToClassifierOptions(c_classifier_options, &cpp_classifier_options); | ||
EXPECT_EQ(cpp_classifier_options.display_names_locale, "en"); | ||
EXPECT_EQ(cpp_classifier_options.max_results, -1); | ||
EXPECT_FLOAT_EQ(cpp_classifier_options.score_threshold, 0.0); | ||
EXPECT_EQ(cpp_classifier_options.category_allowlist, | ||
std::vector<std::string>{}); | ||
EXPECT_EQ(cpp_classifier_options.category_denylist, | ||
std::vector<std::string>{}); | ||
} | ||
|
||
} // namespace mediapipe::tasks::c::components::processors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* Copyright 2023 The MediaPipe Authors. | ||
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 "mediapipe/tasks/c/core/base_options_converter.h" | ||
|
||
#include <string> | ||
|
||
#include "mediapipe/framework/port/gtest.h" | ||
#include "mediapipe/tasks/c/core/base_options.h" | ||
#include "mediapipe/tasks/cc/core/base_options.h" | ||
|
||
namespace mediapipe::tasks::c::core { | ||
|
||
constexpr char kAssetBuffer[] = "abc"; | ||
constexpr char kModelAssetPath[] = "abc.tflite"; | ||
|
||
TEST(BaseOptionsConverterTest, ConvertsBaseOptionsAssetBuffer) { | ||
BaseOptions c_base_options = {/* model_asset_buffer= */ kAssetBuffer, | ||
/* model_asset_path= */ nullptr}; | ||
|
||
mediapipe::tasks::core::BaseOptions cpp_base_options = {}; | ||
|
||
CppConvertToBaseOptions(c_base_options, &cpp_base_options); | ||
EXPECT_EQ(*cpp_base_options.model_asset_buffer, std::string{kAssetBuffer}); | ||
EXPECT_EQ(cpp_base_options.model_asset_path, ""); | ||
} | ||
|
||
TEST(BaseOptionsConverterTest, ConvertsBaseOptionsAssetPath) { | ||
BaseOptions c_base_options = {/* model_asset_buffer= */ nullptr, | ||
/* model_asset_path= */ kModelAssetPath}; | ||
|
||
mediapipe::tasks::core::BaseOptions cpp_base_options = {}; | ||
|
||
CppConvertToBaseOptions(c_base_options, &cpp_base_options); | ||
EXPECT_EQ(cpp_base_options.model_asset_buffer.get(), nullptr); | ||
EXPECT_EQ(cpp_base_options.model_asset_path, std::string{kModelAssetPath}); | ||
} | ||
|
||
} // namespace mediapipe::tasks::c::core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters