Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(677)

Unified Diff: components/translate/core/browser/ranker_model_unittest.cc

Issue 2761363002: [translate] Add ranker model proto and loader. (Closed)
Patch Set: comments from droger@ Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/translate/core/browser/ranker_model_loader_unittest.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/translate/core/browser/ranker_model_unittest.cc
diff --git a/components/translate/core/browser/ranker_model_unittest.cc b/components/translate/core/browser/ranker_model_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..83bab0b38e60e721e7ac3fdc9865f24e31dcce24
--- /dev/null
+++ b/components/translate/core/browser/ranker_model_unittest.cc
@@ -0,0 +1,76 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/translate/core/browser/ranker_model.h"
+
+#include <memory>
+
+#include "base/memory/ptr_util.h"
+#include "base/time/time.h"
+#include "components/translate/core/browser/proto/ranker_model.pb.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+using chrome_intelligence::RankerModel;
+
+const char kModelURL[] = "https://some.url.net/model";
+
+int64_t InSeconds(const base::Time t) {
+ return (t - base::Time()).InSeconds();
+}
+
+std::unique_ptr<RankerModel> NewModel(const std::string& model_url,
+ base::Time last_modified,
+ base::TimeDelta cache_duration) {
+ std::unique_ptr<RankerModel> model = base::MakeUnique<RankerModel>();
+ auto* metadata = model->mutable_proto()->mutable_metadata();
+ if (!model_url.empty())
+ metadata->set_source(model_url);
+ if (!last_modified.is_null())
+ metadata->set_last_modified_sec(InSeconds(last_modified));
+ if (!cache_duration.is_zero())
+ metadata->set_cache_duration_sec(cache_duration.InSeconds());
+
+ auto* translate = model->mutable_proto()->mutable_translate();
+ translate->set_version(1);
+
+ auto* logit = translate->mutable_logistic_regression_model();
+ logit->set_bias(0.1f);
+ logit->set_accept_ratio_weight(0.2f);
+ logit->set_decline_ratio_weight(0.3f);
+ logit->set_ignore_ratio_weight(0.4f);
+ return model;
+}
+
+} // namespace
+
+TEST(RankerModelTest, Serialization) {
+ base::Time last_modified = base::Time::Now();
+ base::TimeDelta cache_duration = base::TimeDelta::FromDays(3);
+ std::unique_ptr<RankerModel> original_model =
+ NewModel(kModelURL, last_modified, cache_duration);
+ std::string original_model_str = original_model->SerializeAsString();
+ std::unique_ptr<RankerModel> serialized_model =
+ RankerModel::FromString(original_model_str);
+ std::string serialized_model_str = serialized_model->SerializeAsString();
+
+ EXPECT_EQ(serialized_model_str, original_model_str);
+ EXPECT_EQ(serialized_model->GetSourceURL(), kModelURL);
+ EXPECT_EQ(serialized_model->proto().metadata().last_modified_sec(),
+ InSeconds(last_modified));
+ EXPECT_EQ(serialized_model->proto().metadata().cache_duration_sec(),
+ cache_duration.InSeconds());
+}
+
+TEST(RankerModelTest, IsExpired) {
+ base::Time today = base::Time::Now();
+ base::TimeDelta days_15 = base::TimeDelta::FromDays(15);
+ base::TimeDelta days_30 = base::TimeDelta::FromDays(30);
+ base::TimeDelta days_60 = base::TimeDelta::FromDays(60);
+
+ EXPECT_FALSE(NewModel(kModelURL, today, days_30)->IsExpired());
+ EXPECT_FALSE(NewModel(kModelURL, today - days_15, days_30)->IsExpired());
+ EXPECT_TRUE(NewModel(kModelURL, today - days_60, days_30)->IsExpired());
+}
« no previous file with comments | « components/translate/core/browser/ranker_model_loader_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698