DoorKeeper/test/unit/saltedid.cpp

55 lines
1.7 KiB
C++

/* Copyright 2015, Michele Santullo
* This file is part of DoorKeeper.
*
* DoorKeeper is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DoorKeeper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DoorKeeper. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include "doorkeeper/implem/idmanager.hpp"
TEST(components, saltedid) {
typedef dk::IDManager<5, 11> id_manager_t;
id_manager_t idman;
EXPECT_EQ(2, sizeof(idman.allocate()));
EXPECT_EQ(1, sizeof(id_manager_t::value_type::salt_type));
EXPECT_EQ(2, sizeof(id_manager_t::value_type::index_type));
EXPECT_EQ(16, id_manager_t::bit_count);
for (unsigned int z = 0; z < 20; ++z) {
auto index = idman.allocate();
EXPECT_EQ(0, index.salt());
EXPECT_EQ(z + 1, index.index());
}
auto free_index = idman.allocate();
EXPECT_EQ(0, free_index.salt());
for (unsigned int z = 0; z < 20; ++z) {
auto index = idman.allocate();
EXPECT_EQ(0, index.salt());
EXPECT_EQ(z + 22, index.index());
}
for (unsigned int z = 0; z < (1 << 5) - 1; ++z) {
idman.free(free_index);
free_index = idman.allocate();
EXPECT_EQ(z + 1, free_index.salt());
EXPECT_EQ(21, free_index.index());
}
idman.free(free_index);
free_index = idman.allocate();
EXPECT_EQ(0, free_index.salt());
EXPECT_EQ(42, free_index.index());
}