blob: bd140a5f73e769184aac4236658251104fe6b6ff [file] [log] [blame]
Curtis McMullan70ece0a2021-01-11 15:21:06 +11001// Copyright 2021 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <gtest/gtest.h>
6
7#include "ml/text_suggestions.h"
8#include "ml/util.h"
9
10namespace ml {
11namespace {
12
13TEST(TextSuggestionsTest, CanLoadLibrary) {
14 auto* const instance = ml::TextSuggestions::GetInstance();
15 if (IsAsan()) {
16 EXPECT_FALSE(ml::TextSuggestions::IsTextSuggestionsSupported());
17 EXPECT_EQ(instance->GetStatus(),
18 ml::TextSuggestions::Status::kNotSupported);
19 return;
20 }
21
22 if (ml::TextSuggestions::IsTextSuggestionsSupported()) {
23 EXPECT_EQ(instance->GetStatus(), ml::TextSuggestions::Status::kOk);
24 } else {
25 EXPECT_EQ(instance->GetStatus(),
26 ml::TextSuggestions::Status::kNotSupported);
27 }
28}
29
30TEST(TextSuggestionsText, ExampleRequest) {
31 auto* const instance = ml::TextSuggestions::GetInstance();
32 if (instance->GetStatus() == ml::TextSuggestions::Status::kNotSupported) {
33 return;
34 }
35
36 ASSERT_EQ(instance->GetStatus(), TextSuggestions::Status::kOk);
37
38 TextSuggester const suggester = instance->CreateTextSuggester();
39 instance->LoadTextSuggester(suggester);
40
41 chrome_knowledge::TextSuggesterRequest request;
42 request.set_text("How are y");
43
44 chrome_knowledge::NextWordCompletionCandidate* candidate =
45 request.add_next_word_candidates();
46 candidate->set_text("you");
47 candidate->set_normalized_score(-1.0f);
48
49 chrome_knowledge::TextSuggesterResult result;
50 instance->GenerateSuggestions(suggester, request, &result);
51
52 ASSERT_GT(result.candidates_size(), 0);
53 EXPECT_EQ(result.candidates(0).has_multi_word(), true);
54 EXPECT_EQ(result.candidates(0).multi_word().text(), "you doing");
55 EXPECT_FLOAT_EQ(result.candidates(0).multi_word().normalized_score(),
56 -0.680989f);
57
58 instance->DestroyTextSuggester(suggester);
59}
60
61} // namespace
62} // namespace ml