003 File Manager
Current Path:
/usr/src/contrib/kyua/utils
usr
/
src
/
contrib
/
kyua
/
utils
/
📁
..
📄
.gitignore
(27 B)
📄
Kyuafile
(651 B)
📄
Makefile.am.inc
(5.42 KB)
📄
auto_array.hpp
(3.32 KB)
📄
auto_array.ipp
(6 KB)
📄
auto_array_fwd.hpp
(1.8 KB)
📄
auto_array_test.cpp
(8.27 KB)
📁
cmdline
📁
config
📄
datetime.cpp
(16.78 KB)
📄
datetime.hpp
(4.57 KB)
📄
datetime_fwd.hpp
(1.83 KB)
📄
datetime_test.cpp
(19.52 KB)
📄
defs.hpp.in
(2.18 KB)
📄
env.cpp
(6.62 KB)
📄
env.hpp
(2.31 KB)
📄
env_test.cpp
(4.97 KB)
📁
format
📁
fs
📁
logging
📄
memory.cpp
(5.29 KB)
📄
memory.hpp
(1.8 KB)
📄
memory_test.cpp
(2.25 KB)
📄
noncopyable.hpp
(2.78 KB)
📄
optional.hpp
(2.97 KB)
📄
optional.ipp
(6.3 KB)
📄
optional_fwd.hpp
(2.14 KB)
📄
optional_test.cpp
(7.83 KB)
📄
passwd.cpp
(5.68 KB)
📄
passwd.hpp
(2.4 KB)
📄
passwd_fwd.hpp
(1.8 KB)
📄
passwd_test.cpp
(5.75 KB)
📁
process
📄
sanity.cpp
(6.48 KB)
📄
sanity.hpp
(6.93 KB)
📄
sanity_fwd.hpp
(1.98 KB)
📄
sanity_test.cpp
(9.36 KB)
📁
signals
📁
sqlite
📄
stacktrace.cpp
(12.56 KB)
📄
stacktrace.hpp
(2.71 KB)
📄
stacktrace_helper.cpp
(1.58 KB)
📄
stacktrace_test.cpp
(21.15 KB)
📄
stream.cpp
(4.59 KB)
📄
stream.hpp
(2.16 KB)
📄
stream_test.cpp
(4.92 KB)
📄
test_utils.ipp
(3.53 KB)
📁
text
📄
units.cpp
(5.25 KB)
📄
units.hpp
(2.99 KB)
📄
units_fwd.hpp
(1.79 KB)
📄
units_test.cpp
(7.79 KB)
Editing: optional_test.cpp
// Copyright 2010 The Kyua Authors. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // * Neither the name of Google Inc. nor the names of its contributors // may be used to endorse or promote products derived from this software // without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "utils/optional.ipp" #include <iostream> #include <sstream> #include <atf-c++.hpp> using utils::none; using utils::optional; namespace { /// Fake class to capture calls to the new and delete operators. class test_alloc { public: /// Value to disambiguate objects after construction. int value; /// Balance of alive instances of this class in dynamic memory. static size_t instances; /// Constructs a new optional object. /// /// \param value_ The value to store in this object for disambiguation. test_alloc(int value_) : value(value_) { } /// Allocates a new object and records its existence. /// /// \param size The amount of memory to allocate. /// /// \return A pointer to the allocated memory. /// /// \throw std::bad_alloc If the memory allocation fails. void* operator new(size_t size) { instances++; std::cout << "test_alloc::operator new called\n"; return ::operator new(size); } /// Deallocates an existing object and unrecords its existence. /// /// \param mem The pointer to the memory to deallocate. void operator delete(void* mem) { instances--; std::cout << "test_alloc::operator delete called\n"; ::operator delete(mem); } }; size_t test_alloc::instances = 0; /// Constructs and returns an optional object. /// /// This is used by tests to validate that returning an object from within a /// function works (i.e. the necessary constructors are available). /// /// \tparam Type The type of the object included in the optional wrapper. /// \param value The value to put inside the optional wrapper. /// /// \return The constructed optional object. template< typename Type > optional< Type > return_optional(const Type& value) { return optional< Type >(value); } } // anonymous namespace ATF_TEST_CASE_WITHOUT_HEAD(ctors__native_type); ATF_TEST_CASE_BODY(ctors__native_type) { const optional< int > no_args; ATF_REQUIRE(!no_args); const optional< int > with_none(none); ATF_REQUIRE(!with_none); const optional< int > with_arg(3); ATF_REQUIRE(with_arg); ATF_REQUIRE_EQ(3, with_arg.get()); const optional< int > copy_none(with_none); ATF_REQUIRE(!copy_none); const optional< int > copy_arg(with_arg); ATF_REQUIRE(copy_arg); ATF_REQUIRE_EQ(3, copy_arg.get()); } ATF_TEST_CASE_WITHOUT_HEAD(ctors__complex_type); ATF_TEST_CASE_BODY(ctors__complex_type) { const optional< std::string > no_args; ATF_REQUIRE(!no_args); const optional< std::string > with_none(none); ATF_REQUIRE(!with_none); const optional< std::string > with_arg("foo"); ATF_REQUIRE(with_arg); ATF_REQUIRE_EQ("foo", with_arg.get()); const optional< std::string > copy_none(with_none); ATF_REQUIRE(!copy_none); const optional< std::string > copy_arg(with_arg); ATF_REQUIRE(copy_arg); ATF_REQUIRE_EQ("foo", copy_arg.get()); } ATF_TEST_CASE_WITHOUT_HEAD(assign); ATF_TEST_CASE_BODY(assign) { optional< int > from_default; from_default = optional< int >(); ATF_REQUIRE(!from_default); optional< int > from_none(3); from_none = none; ATF_REQUIRE(!from_none); optional< int > from_int; from_int = 6; ATF_REQUIRE_EQ(6, from_int.get()); } ATF_TEST_CASE_WITHOUT_HEAD(return); ATF_TEST_CASE_BODY(return) { optional< long > from_return(return_optional< long >(123)); ATF_REQUIRE(from_return); ATF_REQUIRE_EQ(123, from_return.get()); } ATF_TEST_CASE_WITHOUT_HEAD(memory); ATF_TEST_CASE_BODY(memory) { ATF_REQUIRE_EQ(0, test_alloc::instances); { optional< test_alloc > optional1(test_alloc(3)); ATF_REQUIRE_EQ(1, test_alloc::instances); ATF_REQUIRE_EQ(3, optional1.get().value); { optional< test_alloc > optional2(optional1); ATF_REQUIRE_EQ(2, test_alloc::instances); ATF_REQUIRE_EQ(3, optional2.get().value); optional2 = 5; ATF_REQUIRE_EQ(2, test_alloc::instances); ATF_REQUIRE_EQ(5, optional2.get().value); ATF_REQUIRE_EQ(3, optional1.get().value); } ATF_REQUIRE_EQ(1, test_alloc::instances); ATF_REQUIRE_EQ(3, optional1.get().value); } ATF_REQUIRE_EQ(0, test_alloc::instances); } ATF_TEST_CASE_WITHOUT_HEAD(get_default); ATF_TEST_CASE_BODY(get_default) { const std::string def_value = "hello"; optional< std::string > optional; ATF_REQUIRE(&def_value == &optional.get_default(def_value)); optional = "bye"; ATF_REQUIRE_EQ("bye", optional.get_default(def_value)); } ATF_TEST_CASE_WITHOUT_HEAD(make_optional); ATF_TEST_CASE_BODY(make_optional) { optional< int > opt = utils::make_optional(576); ATF_REQUIRE(opt); ATF_REQUIRE_EQ(576, opt.get()); } ATF_TEST_CASE_WITHOUT_HEAD(operators_eq_and_ne); ATF_TEST_CASE_BODY(operators_eq_and_ne) { optional< int > opt1, opt2; opt1 = none; opt2 = none; ATF_REQUIRE( opt1 == opt2); ATF_REQUIRE(!(opt1 != opt2)); opt1 = utils::make_optional(5); opt2 = none; ATF_REQUIRE(!(opt1 == opt2)); ATF_REQUIRE( opt1 != opt2); opt1 = none; opt2 = utils::make_optional(5); ATF_REQUIRE(!(opt1 == opt2)); ATF_REQUIRE( opt1 != opt2); opt1 = utils::make_optional(5); opt2 = utils::make_optional(5); ATF_REQUIRE( opt1 == opt2); ATF_REQUIRE(!(opt1 != opt2)); opt1 = utils::make_optional(6); opt2 = utils::make_optional(5); ATF_REQUIRE(!(opt1 == opt2)); ATF_REQUIRE( opt1 != opt2); } ATF_TEST_CASE_WITHOUT_HEAD(output); ATF_TEST_CASE_BODY(output) { { std::ostringstream str; str << optional< int >(none); ATF_REQUIRE_EQ("none", str.str()); } { std::ostringstream str; str << optional< int >(5); ATF_REQUIRE_EQ("5", str.str()); } { std::ostringstream str; str << optional< std::string >("this is a text"); ATF_REQUIRE_EQ("this is a text", str.str()); } } ATF_INIT_TEST_CASES(tcs) { ATF_ADD_TEST_CASE(tcs, ctors__native_type); ATF_ADD_TEST_CASE(tcs, ctors__complex_type); ATF_ADD_TEST_CASE(tcs, assign); ATF_ADD_TEST_CASE(tcs, return); ATF_ADD_TEST_CASE(tcs, memory); ATF_ADD_TEST_CASE(tcs, get_default); ATF_ADD_TEST_CASE(tcs, make_optional); ATF_ADD_TEST_CASE(tcs, operators_eq_and_ne); ATF_ADD_TEST_CASE(tcs, output); }
Upload File
Create Folder