/*
 * Copyright 2017 The Cartographer Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "cartographer/common/optional.h"

#include "gtest/gtest.h"

namespace cartographer {
namespace common {
namespace {

TEST(OptionalTest, CreateDisengagedObject) {
  const optional<int> o;
  EXPECT_FALSE(o.has_value());
  const optional<float> x;
  EXPECT_FALSE(x.has_value());
}

TEST(OptionalTest, CreateWithValue) {
  const optional<int> a(5);
  EXPECT_TRUE(a.has_value());
  EXPECT_EQ(5, a.value());
}

TEST(OptionalTest, CreateFromOtherOptional) {
  const optional<int> a(5);
  const optional<int> b = a;
  EXPECT_TRUE(a.has_value());
  EXPECT_TRUE(b.has_value());
  EXPECT_EQ(5, a.value());
  EXPECT_EQ(5, b.value());
}

TEST(OptionalTest, AssignmentOperator) {
  optional<int> a(5);
  optional<int> b(4);
  optional<int> c;
  a = b;
  EXPECT_TRUE(a.has_value());
  EXPECT_EQ(4, a.value());
  a = c;
  EXPECT_FALSE(a.has_value());
  a = 3;
  EXPECT_TRUE(a.has_value());
  EXPECT_EQ(3, a.value());
}

}  // namespace
}  // namespace common
}  // namespace cartographer
