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

Side by Side Diff: components/translate/core/browser/ranker_model_unittest.cc

Issue 2761363002: [translate] Add ranker model proto and loader. (Closed)
Patch Set: Initial CL 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium 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 "components/translate/core/browser/ranker_model.h"
6
7 #include <memory>
8
9 #include "base/memory/ptr_util.h"
10 #include "base/time/time.h"
11 #include "components/translate/core/browser/proto/ranker_model.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 using chrome_intelligence::RankerModel;
17
18 const char kModelURL[] = "https://some.url.net/model";
19
20 int64_t InSeconds(const base::Time& t) {
droger 2017/03/22 14:30:07 Time should be passed by value (here and in other
Roger McFarlane (Chromium) 2017/03/22 20:31:43 Done.
21 return (t - base::Time()).InSeconds();
22 }
23
24 std::unique_ptr<RankerModel> NewModel(const std::string& model_url,
25 const base::Time& last_modified,
26 const base::TimeDelta& cache_duration) {
27 auto model = base::MakeUnique<RankerModel>();
28 auto* metadata = model->mutable_proto()->mutable_metadata();
29 if (!model_url.empty())
30 metadata->set_source(model_url);
31 if (!last_modified.is_null()) {
32 auto last_modified_sec = InSeconds(last_modified);
33 metadata->set_last_modified_sec(last_modified_sec);
34 }
35 if (!cache_duration.is_zero())
36 metadata->set_cache_duration_sec(cache_duration.InSeconds());
37
38 auto* translate = model->mutable_proto()->mutable_translate();
39 translate->set_version(1);
40
41 auto* logit = translate->mutable_logistic_regression_model();
42 logit->set_bias(0.1f);
43 logit->set_accept_ratio_weight(0.2f);
44 logit->set_decline_ratio_weight(0.3f);
45 logit->set_ignore_ratio_weight(0.4f);
46 return model;
47 }
48
49 } // namespace
50
51 TEST(RankerModelTest, Serialization) {
52 auto last_modified = base::Time::Now();
53 auto cache_duration = base::TimeDelta::FromDays(3);
54 auto original_model = NewModel(kModelURL, last_modified, cache_duration);
55 auto original_model_as_string = original_model->SerializeAsString();
droger 2017/03/22 14:30:07 Nit, optional: I think the code would be clearer w
Roger McFarlane (Chromium) 2017/03/22 20:31:43 Done.
56 auto serialized_model = RankerModel::FromString(original_model_as_string);
57 auto serialized_model_as_string = serialized_model->SerializeAsString();
58
59 EXPECT_EQ(serialized_model_as_string, original_model_as_string);
60 EXPECT_EQ(serialized_model->GetSourceURL(), kModelURL);
61 EXPECT_EQ(serialized_model->proto().metadata().last_modified_sec(),
62 InSeconds(last_modified));
63 EXPECT_EQ(serialized_model->proto().metadata().cache_duration_sec(),
64 cache_duration.InSeconds());
65 }
66
67 TEST(RankerModelTest, IsExpired) {
68 const auto today = base::Time::Now();
69 const auto days_15 = base::TimeDelta::FromDays(15);
70 const auto days_30 = base::TimeDelta::FromDays(30);
71 const auto days_60 = base::TimeDelta::FromDays(60);
72
73 EXPECT_FALSE(NewModel(kModelURL, today, days_30)->IsExpired());
74 EXPECT_FALSE(NewModel(kModelURL, today - days_15, days_30)->IsExpired());
75 EXPECT_TRUE(NewModel(kModelURL, today - days_60, days_30)->IsExpired());
76 }
OLDNEW
« 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