]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2017 Red Hat, Inc. | |
7 | * | |
8 | * Author: Casey Bodley <cbodley@redhat.com> | |
9 | * | |
10 | * This is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU Lesser General Public | |
12 | * License version 2.1, as published by the Free Software | |
13 | * Foundation. See file COPYING. | |
14 | * | |
15 | */ | |
16 | ||
17 | #include "common/convenience.h" // include first: tests that header is standalone | |
18 | ||
19 | #include <string> | |
20 | #include <boost/optional.hpp> | |
21 | #include <gtest/gtest.h> | |
22 | ||
23 | // A just god would not allow the C++ standard to make taking the | |
24 | // address of member functions in the standard library undefined behavior. | |
25 | static std::string::size_type l(const std::string& s) { | |
26 | return s.size(); | |
27 | } | |
28 | ||
29 | TEST(Convenience, MaybeDo) | |
30 | { | |
31 | boost::optional<std::string> s("qwerty"); | |
32 | boost::optional<std::string> t; | |
33 | auto r = ceph::maybe_do(s, l); | |
34 | EXPECT_TRUE(r); | |
35 | EXPECT_EQ(*r, s->size()); | |
36 | ||
37 | EXPECT_FALSE(ceph::maybe_do(t, l)); | |
38 | } | |
39 | ||
40 | TEST(Convenience, MaybeDoOr) | |
41 | { | |
42 | const boost::optional<std::string> s("qwerty"); | |
43 | const boost::optional<std::string> t; | |
44 | auto r = ceph::maybe_do_or(s, l, 0); | |
45 | EXPECT_EQ(r, s->size()); | |
46 | ||
47 | EXPECT_EQ(ceph::maybe_do_or(t, l, 0u), 0u); | |
48 | } | |
49 | ||
50 | TEST(Convenience, StdMaybeDo) | |
51 | { | |
52 | std::optional<std::string> s("qwerty"); | |
53 | std::optional<std::string> t; | |
54 | auto r = ceph::maybe_do(s, l); | |
55 | EXPECT_TRUE(r); | |
56 | EXPECT_EQ(*r, s->size()); | |
57 | ||
58 | EXPECT_FALSE(ceph::maybe_do(t, l)); | |
59 | } | |
60 | ||
61 | TEST(Convenience, StdMaybeDoOr) | |
62 | { | |
63 | const std::optional<std::string> s("qwerty"); | |
64 | const std::optional<std::string> t; | |
65 | auto r = ceph::maybe_do_or(s, l, 0); | |
66 | EXPECT_EQ(r, s->size()); | |
67 | ||
68 | EXPECT_EQ(ceph::maybe_do_or(t, l, 0u), 0u); | |
69 | } |