andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 +0000 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 10 | #include "common.h" // NOLINT |
| 11 | |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | namespace webrtc { |
| 15 | namespace { |
| 16 | |
| 17 | struct MyExperiment { |
| 18 | enum { kDefaultFactor = 1 }; |
| 19 | enum { kDefaultOffset = 2 }; |
| 20 | |
| 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 | |
| 31 | TEST(Config, ReturnsDefaultInstanceIfNotConfigured) { |
| 32 | Config config; |
| 33 | const MyExperiment& my_exp = config.Get<MyExperiment>(); |
| 34 | EXPECT_EQ(MyExperiment::kDefaultFactor, my_exp.factor); |
| 35 | EXPECT_EQ(MyExperiment::kDefaultOffset, my_exp.offset); |
| 36 | } |
| 37 | |
| 38 | TEST(Config, ReturnOptionWhenSet) { |
| 39 | Config config; |
| 40 | config.Set<MyExperiment>(new MyExperiment(5, 1)); |
| 41 | const MyExperiment& my_exp = config.Get<MyExperiment>(); |
| 42 | EXPECT_EQ(5, my_exp.factor); |
| 43 | EXPECT_EQ(1, my_exp.offset); |
| 44 | } |
| 45 | |
| 46 | TEST(Config, SetNullSetsTheOptionBackToDefault) { |
| 47 | Config config; |
| 48 | config.Set<MyExperiment>(new MyExperiment(5, 1)); |
| 49 | config.Set<MyExperiment>(NULL); |
| 50 | const MyExperiment& my_exp = config.Get<MyExperiment>(); |
| 51 | EXPECT_EQ(MyExperiment::kDefaultFactor, my_exp.factor); |
| 52 | EXPECT_EQ(MyExperiment::kDefaultOffset, my_exp.offset); |
| 53 | } |
| 54 | |
| 55 | struct Algo1_CostFunction { |
| 56 | Algo1_CostFunction() {} |
| 57 | |
| 58 | virtual int cost(int x) const { |
| 59 | return x; |
| 60 | } |
| 61 | |
| 62 | virtual ~Algo1_CostFunction() {} |
| 63 | }; |
| 64 | |
| 65 | struct SqrCost : Algo1_CostFunction { |
| 66 | virtual int cost(int x) const { |
| 67 | return x*x; |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | TEST(Config, SupportsPolimorphism) { |
| 72 | Config config; |
| 73 | config.Set<Algo1_CostFunction>(new SqrCost()); |
| 74 | EXPECT_EQ(25, config.Get<Algo1_CostFunction>().cost(5)); |
| 75 | } |
| 76 | } // namespace |
| 77 | } // namespace webrtc |