view tests/libserver/id/main.cpp @ 42:a47a4477f347

Misc: new style, closes #578
author David Demelier <markand@malikania.fr>
date Tue, 29 Nov 2016 21:21:36 +0100
parents d4f5f7231b84
children fabbe1759cec
line wrap: on
line source

/*
 * main.cpp -- test Id
 *
 * Copyright (c) 2013-2016 Malikania Authors
 *
 * 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>

#include <gtest/gtest.h>

#include <malikania/id.hpp>

using namespace malikania;

/*
 * Basic use case.
 * ------------------------------------------------------------------
 */

class test_id : public testing::Test {
protected:
    id_generator<unsigned> m_idgen;
};

TEST_F(test_id, simple)
{
    auto i0 = m_idgen.next();
    auto i1 = m_idgen.next();
    auto i2 = m_idgen.next();

    ASSERT_EQ(0U, i0->value());
    ASSERT_EQ(1U, i1->value());
    ASSERT_EQ(2U, i2->value());
}

TEST_F(test_id, 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.
     */
    ASSERT_EQ(1U, n1->value());
    ASSERT_EQ(3U, n2->value());
}

TEST_F(test_id, 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();

    ASSERT_EQ(1U, n1->value());
    ASSERT_EQ(0U, n2->value());
    ASSERT_EQ(3U, n3->value());
}

/*
 * Limit test.
 * ------------------------------------------------------------------
 */

TEST(Limits, 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) {
        FAIL() << ex.what();
    }

    ASSERT_EQ(127U, ids.size());
    ASSERT_EQ(126, ids[126]->value());
}

TEST(Limits, fail)
{
    id_generator<std::int8_t> idgen;
    std::vector<std::unique_ptr<id<std::int8_t>>> ids;

    ASSERT_ANY_THROW(
        for (int i = 0; i < 200; ++i) {
            ids.push_back(idgen.next());
        }
    );

    ASSERT_EQ(127U, ids.size());
    ASSERT_EQ(126, ids[126]->value());
}

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}