susumu.yata
null+****@clear*****
Tue May 28 17:34:35 JST 2013
susumu.yata 2013-05-28 17:34:35 +0900 (Tue, 28 May 2013) New Revision: 59c63489aa808efd2f35f7fa050273753c059c62 https://github.com/groonga/grnxx/commit/59c63489aa808efd2f35f7fa050273753c059c62 Message: Add tests for grnxx::BitArray. Added files: test/test_bit_array.cpp Modified files: .gitignore test/Makefile.am Modified: .gitignore (+1 -0) =================================================================== --- .gitignore 2013-05-28 17:21:54 +0900 (2b0f2f4) +++ .gitignore 2013-05-28 17:34:35 +0900 (4bdac9e) @@ -31,6 +31,7 @@ test/test_alpha_double_array test/test_alpha_map test/test_array test/test_backtrace +test/test_bit_array test/test_bytes test/test_charset test/test_db_blob_vector Modified: test/Makefile.am (+4 -0) =================================================================== --- test/Makefile.am 2013-05-28 17:21:54 +0900 (85ac15a) +++ test/Makefile.am 2013-05-28 17:34:35 +0900 (325145e) @@ -5,6 +5,7 @@ TESTS = \ test_alpha_map \ test_array \ test_backtrace \ + test_bit_array \ test_bytes \ test_charset \ test_db_blob_vector \ @@ -46,6 +47,9 @@ test_array_LDADD = ../lib/grnxx/libgrnxx.la test_backtrace_SOURCES = test_backtrace.cpp test_backtrace_LDADD = ../lib/grnxx/libgrnxx.la +test_bit_array_SOURCES = test_bit_array.cpp +test_bit_array_LDADD = ../lib/grnxx/libgrnxx.la + test_bytes_SOURCES = test_bytes.cpp test_bytes_LDADD = ../lib/grnxx/libgrnxx.la Added: test/test_bit_array.cpp (+117 -0) 100644 =================================================================== --- /dev/null +++ test/test_bit_array.cpp 2013-05-28 17:34:35 +0900 (434557e) @@ -0,0 +1,117 @@ +/* + Copyright (C) 2012-2013 Brazil, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include <cassert> +#include <memory> +#include <random> +#include <vector> + +#include "grnxx/bit_array.hpp" +#include "grnxx/logger.hpp" +#include "grnxx/storage.hpp" + +namespace { + +std::mt19937_64 mersenne_twister; + +template <std::uint64_t PAGE_SIZE, + std::uint64_t TABLE_SIZE, + std::uint64_t SECONDARY_TABLE_SIZE> +void test_bit_array() { + GRNXX_NOTICE() << __PRETTY_FUNCTION__; + + using BitArray = grnxx::BitArray<PAGE_SIZE, TABLE_SIZE, SECONDARY_TABLE_SIZE>; + using Unit = typename BitArray::Unit; + + // Generate an array of random units. + std::vector<Unit> units(BitArray::size() / BitArray::unit_size()); + for (std::size_t i = 0; i < units.size(); ++i) { + units[i] = mersenne_twister(); + } + + // Create an anonymous Storage. + std::unique_ptr<grnxx::Storage> storage(grnxx::Storage::create(nullptr)); + grnxx::BitArray<PAGE_SIZE, TABLE_SIZE, SECONDARY_TABLE_SIZE> array; + std::uint32_t storage_node_id; + + // Create an Array and test its member functions. + assert(array.create(storage.get(), grnxx::STORAGE_ROOT_NODE_ID)); + assert(array); + assert(array.page_size() == PAGE_SIZE); + assert(array.table_size() == TABLE_SIZE); + assert(array.secondary_table_size() == SECONDARY_TABLE_SIZE); + assert(array.size() == (PAGE_SIZE * TABLE_SIZE * SECONDARY_TABLE_SIZE)); + storage_node_id = array.storage_node_id(); + + for (std::uint64_t i = 0; i < array.size(); ++i) { + const bool bit = (units[i / 64] >> (i % 64)) & 1; + assert(array.set(i, bit)); + } + for (std::uint64_t i = 0; i < array.size(); ++i) { + const bool expected_bit = (units[i / 64] >> (i % 64)) & 1; + bool stored_bit; + assert(array.get(i, &stored_bit)); + assert(stored_bit == expected_bit); + } + for (std::uint64_t i = 0; i < array.size(); ++i) { + const bool expected_bit = (units[i / 64] >> (i % 64)) & 1; + assert(array[i] == expected_bit); + } + for (std::uint64_t i = 0; i < (array.size() / array.unit_size()); ++i) { + const Unit * const unit = array.get_unit(i); + assert(unit); + assert(*unit == units[i]); + } + for (std::uint64_t i = 0; i < (array.size() / array.page_size()); ++i) { + assert(array.get_page(i)); + } + + // Open the Array. + assert(array.open(storage.get(), storage_node_id)); + for (std::uint64_t i = 0; i < array.size(); ++i) { + const bool expected_bit = (units[i / 64] >> (i % 64)) & 1; + bool stored_bit; + assert(array.get(i, &stored_bit)); + assert(stored_bit == expected_bit); + } + + // Create an Array with the default bit. + assert(array.create(storage.get(), grnxx::STORAGE_ROOT_NODE_ID, false)); + assert(array); + for (std::uint64_t i = 0; i < array.size(); ++i) { + assert(!array[i]); + } + assert(array.create(storage.get(), grnxx::STORAGE_ROOT_NODE_ID, true)); + assert(array); + for (std::uint64_t i = 0; i < array.size(); ++i) { + assert(array[i]); + } +} + +} // namesapce + +int main() { + grnxx::Logger::set_flags(grnxx::LOGGER_WITH_ALL | + grnxx::LOGGER_ENABLE_COUT); + grnxx::Logger::set_max_level(grnxx::NOTICE_LOGGER); + + test_bit_array<256, 1, 1>(); + test_bit_array<256, 64, 1>(); + test_bit_array<256, 64, 16>(); + + return 0; +} -------------- next part -------------- HTML����������������������������... 下載