Mercurial > malikania
view tests/libserver/id/main.cpp @ 140:a83fff870983
Client: prepare state, #712
Add update and draw function in states to allow future development of predefined
states.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 27 Sep 2017 21:02:41 +0200 |
parents | 119bcc5a727e |
children | 4b292c20124c |
line wrap: on
line source
/* * main.cpp -- test Id * * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <cstdint> #include <thread> #define BOOST_TEST_MODULE "Id" #include <boost/test/unit_test.hpp> #include <malikania/id.hpp> using namespace mlk; /* * Basic use case. * ------------------------------------------------------------------ */ class test_id { protected: id_generator<unsigned> m_idgen; }; BOOST_FIXTURE_TEST_SUITE(test_id_suite, test_id) BOOST_AUTO_TEST_CASE(simple) { auto i0 = m_idgen.next(); auto i1 = m_idgen.next(); auto i2 = m_idgen.next(); BOOST_REQUIRE_EQUAL(0U, i0->value()); BOOST_REQUIRE_EQUAL(1U, i1->value()); BOOST_REQUIRE_EQUAL(2U, i2->value()); } BOOST_AUTO_TEST_CASE(release1) { auto i0 = m_idgen.next(); // 0 auto i1 = m_idgen.next(); // 1 auto i2 = m_idgen.next(); // 2 i1 = nullptr; auto n1 = m_idgen.next(); auto n2 = m_idgen.next(); /* * 0 and 2 are currently in use. * * The next id must be 1 and then 3. */ BOOST_REQUIRE_EQUAL(1U, n1->value()); BOOST_REQUIRE_EQUAL(3U, n2->value()); } BOOST_AUTO_TEST_CASE(release2) { auto i0 = m_idgen.next(); // 0 auto i1 = m_idgen.next(); // 1 auto i2 = m_idgen.next(); // 2 i1 = nullptr; i0 = nullptr; /* * Only 2 is in use, next id must be: * * - 1 * - 0 * - 3 */ auto n1 = m_idgen.next(); auto n2 = m_idgen.next(); auto n3 = m_idgen.next(); BOOST_REQUIRE_EQUAL(1U, n1->value()); BOOST_REQUIRE_EQUAL(0U, n2->value()); BOOST_REQUIRE_EQUAL(3U, n3->value()); } /* * Limit test. * ------------------------------------------------------------------ */ BOOST_AUTO_TEST_CASE(max) { id_generator<std::int8_t> idgen; std::vector<std::unique_ptr<id<std::int8_t>>> ids; try { for (int i = 0; i < 127; ++i) { ids.push_back(idgen.next()); } } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } BOOST_REQUIRE_EQUAL(127U, ids.size()); BOOST_REQUIRE_EQUAL(126, ids[126]->value()); } BOOST_AUTO_TEST_CASE(fail) { id_generator<std::int8_t> idgen; std::vector<std::unique_ptr<id<std::int8_t>>> ids; BOOST_REQUIRE_THROW({ for (int i = 0; i < 200; ++i) { ids.push_back(idgen.next()); } }, std::exception); BOOST_REQUIRE_EQUAL(127U, ids.size()); BOOST_REQUIRE_EQUAL(126, ids[126]->value()); } BOOST_AUTO_TEST_SUITE_END()