summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_convenience.cc
blob: 4f4ba386e1c9619777230f06c0aec5d09dd5ea54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2017 Red Hat, Inc.
 *
 * Author: Casey Bodley <cbodley@redhat.com>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "common/convenience.h" // include first: tests that header is standalone

#include <string>
#include <boost/optional.hpp>
#include <gtest/gtest.h>

// A just god would not allow the C++ standard to make taking the
// address of member functions in the standard library undefined behavior.
static std::string::size_type l(const std::string& s) {
  return s.size();
}

TEST(Convenience, MaybeDo)
{
  boost::optional<std::string> s("qwerty");
  boost::optional<std::string> t;
  auto r = ceph::maybe_do(s, l);
  EXPECT_TRUE(r);
  EXPECT_EQ(*r, s->size());

  EXPECT_FALSE(ceph::maybe_do(t, l));
}

TEST(Convenience, MaybeDoOr)
{
  const boost::optional<std::string> s("qwerty");
  const boost::optional<std::string> t;
  auto r = ceph::maybe_do_or(s, l, 0);
  EXPECT_EQ(r, s->size());

  EXPECT_EQ(ceph::maybe_do_or(t, l, 0u), 0u);
}

TEST(Convenience, StdMaybeDo)
{
  std::optional<std::string> s("qwerty");
  std::optional<std::string> t;
  auto r = ceph::maybe_do(s, l);
  EXPECT_TRUE(r);
  EXPECT_EQ(*r, s->size());

  EXPECT_FALSE(ceph::maybe_do(t, l));
}

TEST(Convenience, StdMaybeDoOr)
{
  const std::optional<std::string> s("qwerty");
  const std::optional<std::string> t;
  auto r = ceph::maybe_do_or(s, l, 0);
  EXPECT_EQ(r, s->size());

  EXPECT_EQ(ceph::maybe_do_or(t, l, 0u), 0u);
}