blob: 082c18c2c7d9361fde111a1880e078bbf050edb7 [file] [log] [blame]
andresp@webrtc.org1dba6212013-05-13 08:06:36 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
pbos@webrtc.org36561922013-06-27 10:18:09 +000010#include "webrtc/common.h"
andresp@webrtc.org1dba6212013-05-13 08:06:36 +000011
pbos@webrtc.org36561922013-06-27 10:18:09 +000012#include "testing/gtest/include/gtest/gtest.h"
andresp@webrtc.org1dba6212013-05-13 08:06:36 +000013
14namespace webrtc {
15namespace {
16
17struct MyExperiment {
pbos@webrtc.org36561922013-06-27 10:18:09 +000018 static const int kDefaultFactor;
19 static const int kDefaultOffset;
andresp@webrtc.org1dba6212013-05-13 08:06:36 +000020
21 MyExperiment()
22 : factor(kDefaultFactor), offset(kDefaultOffset) {}
23
24 MyExperiment(int factor, int offset)
25 : factor(factor), offset(offset) {}
26
27 int factor;
28 int offset;
29};
30
pbos@webrtc.org36561922013-06-27 10:18:09 +000031const int MyExperiment::kDefaultFactor = 1;
32const int MyExperiment::kDefaultOffset = 2;
33
andresp@webrtc.org1dba6212013-05-13 08:06:36 +000034TEST(Config, ReturnsDefaultInstanceIfNotConfigured) {
35 Config config;
36 const MyExperiment& my_exp = config.Get<MyExperiment>();
37 EXPECT_EQ(MyExperiment::kDefaultFactor, my_exp.factor);
38 EXPECT_EQ(MyExperiment::kDefaultOffset, my_exp.offset);
39}
40
41TEST(Config, ReturnOptionWhenSet) {
42 Config config;
43 config.Set<MyExperiment>(new MyExperiment(5, 1));
44 const MyExperiment& my_exp = config.Get<MyExperiment>();
45 EXPECT_EQ(5, my_exp.factor);
46 EXPECT_EQ(1, my_exp.offset);
47}
48
49TEST(Config, SetNullSetsTheOptionBackToDefault) {
50 Config config;
51 config.Set<MyExperiment>(new MyExperiment(5, 1));
52 config.Set<MyExperiment>(NULL);
53 const MyExperiment& my_exp = config.Get<MyExperiment>();
54 EXPECT_EQ(MyExperiment::kDefaultFactor, my_exp.factor);
55 EXPECT_EQ(MyExperiment::kDefaultOffset, my_exp.offset);
56}
57
58struct Algo1_CostFunction {
59 Algo1_CostFunction() {}
60
61 virtual int cost(int x) const {
62 return x;
63 }
64
65 virtual ~Algo1_CostFunction() {}
66};
67
68struct SqrCost : Algo1_CostFunction {
69 virtual int cost(int x) const {
70 return x*x;
71 }
72};
73
pbos@webrtc.org36561922013-06-27 10:18:09 +000074TEST(Config, SupportsPolymorphism) {
andresp@webrtc.org1dba6212013-05-13 08:06:36 +000075 Config config;
76 config.Set<Algo1_CostFunction>(new SqrCost());
77 EXPECT_EQ(25, config.Get<Algo1_CostFunction>().cost(5));
78}
79} // namespace
80} // namespace webrtc