view cpp/to_int/test/main.cpp @ 653:87e1f4c7da76

misc: happy new year!
author David Demelier <markand@malikania.fr>
date Tue, 08 Jan 2019 21:19:17 +0100
parents 5bd9424a523a
children
line wrap: on
line source

/*
 * main.cpp -- test to_int functions
 *
 * Copyright (c) 2017-2019 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.
 */

#define BOOST_TEST_MODULE "to-int"
#include <boost/test/unit_test.hpp>

#include "to_int.hpp"

BOOST_AUTO_TEST_SUITE(valid)

BOOST_AUTO_TEST_CASE(signed_to_int)
{
	BOOST_TEST(*to_int("10") == 10);
	BOOST_TEST(*to_int<std::int8_t>("-10") == -10);
	BOOST_TEST(*to_int<std::int8_t>("10") == 10);
	BOOST_TEST(*to_int<std::int16_t>("-1000") == -1000);
	BOOST_TEST(*to_int<std::int16_t>("1000") == 1000);
	BOOST_TEST(*to_int<std::int32_t>("-1000") == -1000);
	BOOST_TEST(*to_int<std::int32_t>("1000") == 1000);
}

BOOST_AUTO_TEST_CASE(signed_to_int64)
{
	BOOST_TEST(*to_int<std::int64_t>("-9223372036854775807") == -9223372036854775807LL);
	BOOST_TEST(*to_int<std::int64_t>("9223372036854775807") == 9223372036854775807LL);
}

BOOST_AUTO_TEST_CASE(unsigned_to_uint)
{
	BOOST_TEST(*to_uint("10") == 10U);
	BOOST_TEST(*to_uint<std::uint8_t>("10") == 10U);
	BOOST_TEST(*to_uint<std::uint16_t>("1000") == 1000U);
	BOOST_TEST(*to_uint<std::uint32_t>("1000") == 1000U);
}

BOOST_AUTO_TEST_CASE(unsigned_to_uint64)
{
	BOOST_TEST(*to_uint<std::uint64_t>("18446744073709551615") == 18446744073709551615ULL);
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(errors)

BOOST_AUTO_TEST_CASE(invalid_argument)
{
	BOOST_TEST(!to_int("plopation"));
	BOOST_TEST(!to_uint("plopation"));
}

BOOST_AUTO_TEST_CASE(out_of_range)
{
	BOOST_TEST(!to_int<std::int8_t>("1000"));
	BOOST_TEST(!to_int<std::int8_t>("-1000"));
	BOOST_TEST(!to_uint<std::uint8_t>("1000"));
	BOOST_TEST(!to_uint<std::uint8_t>("-1000"));
}

BOOST_AUTO_TEST_SUITE_END()