andresp@webrtc.org | 6b68c28 | 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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "modules/audio_processing/include/config.h" |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 11 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 12 | #include "test/gtest.h" |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 13 | |
| 14 | namespace webrtc { |
| 15 | namespace { |
| 16 | |
| 17 | struct MyExperiment { |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 18 | static const ConfigOptionID identifier = ConfigOptionID::kMyExperimentForTest; |
pbos@webrtc.org | 2de80dd | 2013-06-27 10:18:09 +0000 | [diff] [blame] | 19 | static const int kDefaultFactor; |
| 20 | static const int kDefaultOffset; |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 21 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 22 | MyExperiment() : factor(kDefaultFactor), offset(kDefaultOffset) {} |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 23 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 24 | MyExperiment(int factor, int offset) : factor(factor), offset(offset) {} |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 25 | |
| 26 | int factor; |
| 27 | int offset; |
| 28 | }; |
| 29 | |
pbos@webrtc.org | 2de80dd | 2013-06-27 10:18:09 +0000 | [diff] [blame] | 30 | const int MyExperiment::kDefaultFactor = 1; |
| 31 | const int MyExperiment::kDefaultOffset = 2; |
| 32 | |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 33 | TEST(Config, ReturnsDefaultInstanceIfNotConfigured) { |
| 34 | Config config; |
| 35 | const MyExperiment& my_exp = config.Get<MyExperiment>(); |
| 36 | EXPECT_EQ(MyExperiment::kDefaultFactor, my_exp.factor); |
| 37 | EXPECT_EQ(MyExperiment::kDefaultOffset, my_exp.offset); |
| 38 | } |
| 39 | |
| 40 | TEST(Config, ReturnOptionWhenSet) { |
| 41 | Config config; |
| 42 | config.Set<MyExperiment>(new MyExperiment(5, 1)); |
| 43 | const MyExperiment& my_exp = config.Get<MyExperiment>(); |
| 44 | EXPECT_EQ(5, my_exp.factor); |
| 45 | EXPECT_EQ(1, my_exp.offset); |
| 46 | } |
| 47 | |
| 48 | TEST(Config, SetNullSetsTheOptionBackToDefault) { |
| 49 | Config config; |
| 50 | config.Set<MyExperiment>(new MyExperiment(5, 1)); |
| 51 | config.Set<MyExperiment>(NULL); |
| 52 | const MyExperiment& my_exp = config.Get<MyExperiment>(); |
| 53 | EXPECT_EQ(MyExperiment::kDefaultFactor, my_exp.factor); |
| 54 | EXPECT_EQ(MyExperiment::kDefaultOffset, my_exp.offset); |
| 55 | } |
| 56 | |
| 57 | struct Algo1_CostFunction { |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 58 | static const ConfigOptionID identifier = |
| 59 | ConfigOptionID::kAlgo1CostFunctionForTest; |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 60 | Algo1_CostFunction() {} |
| 61 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 62 | virtual int cost(int x) const { return x; } |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 63 | |
| 64 | virtual ~Algo1_CostFunction() {} |
| 65 | }; |
| 66 | |
| 67 | struct SqrCost : Algo1_CostFunction { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 68 | virtual int cost(int x) const { return x * x; } |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
pbos@webrtc.org | 2de80dd | 2013-06-27 10:18:09 +0000 | [diff] [blame] | 71 | TEST(Config, SupportsPolymorphism) { |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 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 |