18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)/*
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *  Use of this source code is governed by a BSD-style license
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *  that can be found in the LICENSE file in the root of the source
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *  tree. An additional intellectual property rights grant can be found
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *  in the file PATENTS.  All contributing project authors may
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *  be found in the AUTHORS file in the root of the source tree.
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) */
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "webrtc/common.h"
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace webrtc {
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace {
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)struct MyExperiment {
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  static const int kDefaultFactor;
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  static const int kDefaultOffset;
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  MyExperiment()
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : factor(kDefaultFactor), offset(kDefaultOffset) {}
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  MyExperiment(int factor, int offset)
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : factor(factor), offset(offset) {}
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int factor;
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int offset;
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)const int MyExperiment::kDefaultFactor = 1;
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)const int MyExperiment::kDefaultOffset = 2;
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(Config, ReturnsDefaultInstanceIfNotConfigured) {
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Config config;
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const MyExperiment& my_exp = config.Get<MyExperiment>();
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(MyExperiment::kDefaultFactor, my_exp.factor);
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(MyExperiment::kDefaultOffset, my_exp.offset);
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(Config, ReturnOptionWhenSet) {
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Config config;
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  config.Set<MyExperiment>(new MyExperiment(5, 1));
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const MyExperiment& my_exp = config.Get<MyExperiment>();
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(5, my_exp.factor);
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1, my_exp.offset);
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(Config, SetNullSetsTheOptionBackToDefault) {
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Config config;
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  config.Set<MyExperiment>(new MyExperiment(5, 1));
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  config.Set<MyExperiment>(NULL);
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const MyExperiment& my_exp = config.Get<MyExperiment>();
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(MyExperiment::kDefaultFactor, my_exp.factor);
558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(MyExperiment::kDefaultOffset, my_exp.offset);
568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)struct Algo1_CostFunction {
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Algo1_CostFunction() {}
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual int cost(int x) const {
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return x;
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual ~Algo1_CostFunction() {}
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)struct SqrCost : Algo1_CostFunction {
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual int cost(int x) const {
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return x*x;
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(Config, SupportsPolymorphism) {
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Config config;
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  config.Set<Algo1_CostFunction>(new SqrCost());
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(25, config.Get<Algo1_CostFunction>().cost(5));
788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace webrtc
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)