view C++/tests/Flags/main.cpp @ 334:0b576ee64d45

* Create brand new hierarchy * Rename DynLib to Dynlib * Remove some warnings
author David Demelier <markand@malikania.fr>
date Sun, 08 Mar 2015 14:26:33 +0100
parents C++/Tests/Flags/main.cpp@0c7bc55e0d36
children d5ec1174b707
line wrap: on
line source

/*
 * main.cpp -- main test file for Flags
 *
 * Copyright (c) 2013, 2014 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 <gtest/gtest.h>

#include <Flags.h>

enum Standard : uint8_t {
	Fullscreen	= (1 << 0),
	Audio		= (1 << 1)
};

enum class Strong : uint8_t {
	NoLog		= (1 << 0),
	NoCheck		= (1 << 1)
};

/* --------------------------------------------------------
 * Global operators on standard enum
 * -------------------------------------------------------- */

TEST(OperatorsStandard, opand)
{
	Standard s(Fullscreen | Audio);

	ASSERT_EQ(Fullscreen, (s & Fullscreen));
	ASSERT_EQ(Audio, (s & Audio));
}

TEST(OperatorsStandard, opor)
{
	Standard s(Fullscreen);

	ASSERT_EQ(3, (s | Audio));
}

TEST(OperatorsStandard, opxor)
{
	Standard s(Fullscreen);

	ASSERT_EQ(3, (s ^ Audio));
}

TEST(OperatorsStandard, opnot)
{
	Standard s(Fullscreen);

	ASSERT_EQ(254, ~s);
}

/* --------------------------------------------------------
 * Global operators on strong enum
 * -------------------------------------------------------- */

TEST(OperatorsStrong, opand)
{
	Strong s(Strong::NoLog | Strong::NoCheck);

	ASSERT_EQ(Strong::NoLog, (s & Strong::NoLog));
	ASSERT_EQ(Strong::NoCheck, (s & Strong::NoCheck));
}

TEST(OperatorsStrong, opor)
{
	Strong s(Strong::NoLog);

	ASSERT_EQ(3, static_cast<int>((s | Strong::NoCheck)));
}

TEST(OperatorsStrong, opxor)
{
	Strong s(Strong::NoLog);

	ASSERT_EQ(3, static_cast<int>((s ^ Strong::NoCheck)));
}

TEST(OperatorsStrong, opnot)
{
	Strong s(Strong::NoLog);

	ASSERT_EQ(254, static_cast<int>(~s));
}

/* --------------------------------------------------------
 * Flags with standard enums
 * -------------------------------------------------------- */

TEST(Standard, construct)
{
	Flags<Standard> s;

	ASSERT_FALSE(s);
	ASSERT_TRUE(!s);

	Flags <Standard> s2(Fullscreen | Audio);

	ASSERT_TRUE(s2);
	ASSERT_FALSE(!s2);
	ASSERT_TRUE(s2 & Fullscreen);
	ASSERT_TRUE(s2 & Audio);
	ASSERT_TRUE((s2 & Fullscreen) == Fullscreen);
	ASSERT_TRUE((s2 & Audio) == Audio);
	ASSERT_TRUE(s2 == (Audio | Fullscreen));
}

TEST(Standard, addByOne)
{
	Flags<Standard> s;

	ASSERT_FALSE(s);
	ASSERT_TRUE(!s);
	ASSERT_TRUE(!(s & Fullscreen));
	ASSERT_TRUE(!(s & Audio));

	s |= Fullscreen;
	ASSERT_TRUE(s);
	ASSERT_FALSE(!s);
	ASSERT_TRUE(s & Fullscreen);
	ASSERT_TRUE((s & Fullscreen) == Fullscreen);
	ASSERT_FALSE(s & Audio);
	ASSERT_FALSE((s & Audio) == Audio);
	ASSERT_TRUE(s == Fullscreen);

	s |= Audio;
	ASSERT_TRUE(s);
	ASSERT_FALSE(!s);
	ASSERT_TRUE(s & Fullscreen);
	ASSERT_TRUE((s & Fullscreen) == Fullscreen);
	ASSERT_TRUE(s & Audio);
	ASSERT_TRUE((s & Audio) == Audio);
	ASSERT_TRUE(s == (Fullscreen | Audio));
}

TEST(Standard, add)
{
	Flags<Standard> s;

	s |= Fullscreen | Audio;
	ASSERT_TRUE(s & (Fullscreen | Audio));
	ASSERT_TRUE((s & (Fullscreen | Audio)) == (Fullscreen | Audio));
}

TEST(Standard, removeByOne)
{
	Flags<Standard> s(Fullscreen | Audio);

	s &= ~(Fullscreen);
	ASSERT_TRUE(s);
	ASSERT_FALSE(!s);
	ASSERT_FALSE(s & Fullscreen);
	ASSERT_FALSE((s & Fullscreen) == Fullscreen);
	ASSERT_TRUE(s & Audio);
	ASSERT_TRUE((s & Audio) == Audio);

	s &= ~(Audio);
	ASSERT_FALSE(s);
	ASSERT_TRUE(!s);
}

TEST(Standard, remove)
{
	Flags<Standard> s(Fullscreen | Audio);

	s &= ~(Fullscreen | Audio);
	ASSERT_FALSE(s);
	ASSERT_TRUE(!s);
}

TEST(Standard, xorAdd)
{
	Flags<Standard> s(Fullscreen | Audio);

	s ^= Audio;
	ASSERT_TRUE(s & Fullscreen);
	ASSERT_TRUE((s & Fullscreen) == Fullscreen);
	ASSERT_FALSE(s & Audio);
	ASSERT_FALSE((s & Audio) == Audio);
}

/* --------------------------------------------------------
 * Flags with strong enums
 * -------------------------------------------------------- */

TEST(Strong, construct)
{
	Flags<Strong> s;

	ASSERT_FALSE(s);
	ASSERT_TRUE(!s);

	Flags <Strong> s2(Strong::NoLog | Strong::NoCheck);

	ASSERT_TRUE(s2);
	ASSERT_FALSE(!s2);
	ASSERT_TRUE(s2 & Strong::NoLog);
	ASSERT_TRUE(s2 & Strong::NoCheck);
	ASSERT_TRUE((s2 & Strong::NoLog) == Strong::NoLog);
	ASSERT_TRUE((s2 & Strong::NoCheck) == Strong::NoCheck);
	ASSERT_TRUE(s2 == (Strong::NoLog | Strong::NoCheck));
}

TEST(Strong, addByOne)
{
	Flags<Strong> s;

	ASSERT_FALSE(s);
	ASSERT_TRUE(!s);
	ASSERT_TRUE(!(s & Strong::NoLog));
	ASSERT_TRUE(!(s & Strong::NoCheck));

	s |= Strong::NoLog;
	ASSERT_TRUE(s);
	ASSERT_FALSE(!s);
	ASSERT_TRUE(s & Strong::NoLog);
	ASSERT_TRUE((s & Strong::NoLog) == Strong::NoLog);
	ASSERT_FALSE(s & Strong::NoCheck);
	ASSERT_FALSE((s & Strong::NoCheck) == Strong::NoCheck);
	ASSERT_TRUE(s == Strong::NoLog);

	s |= Strong::NoCheck;
	ASSERT_TRUE(s);
	ASSERT_FALSE(!s);
	ASSERT_TRUE(s & Strong::NoLog);
	ASSERT_TRUE((s & Strong::NoLog) == Strong::NoLog);
	ASSERT_TRUE(s & Strong::NoCheck);
	ASSERT_TRUE((s & Strong::NoCheck) == Strong::NoCheck);
	ASSERT_TRUE(s == (Strong::NoLog | Strong::NoCheck));
}

TEST(Strong, add)
{
	Flags<Strong> s;

	s |= Strong::NoLog | Strong::NoCheck;
	ASSERT_TRUE(s & (Strong::NoLog | Strong::NoCheck));
	ASSERT_TRUE((s & (Strong::NoLog | Strong::NoCheck)) == (Strong::NoLog | Strong::NoCheck));
}

TEST(Strong, removeByOne)
{
	Flags<Strong> s(Strong::NoLog | Strong::NoCheck);

	s &= ~(Strong::NoLog);
	ASSERT_TRUE(s);
	ASSERT_FALSE(!s);
	ASSERT_FALSE(s & Strong::NoLog);
	ASSERT_FALSE((s & Strong::NoLog) == Strong::NoLog);
	ASSERT_TRUE(s & Strong::NoCheck);
	ASSERT_TRUE((s & Strong::NoCheck) == Strong::NoCheck);

	s &= ~(Strong::NoCheck);
	ASSERT_FALSE(s);
	ASSERT_TRUE(!s);
}

TEST(Strong, remove)
{
	Flags<Strong> s(Strong::NoLog | Strong::NoCheck);

	s &= ~(Strong::NoLog | Strong::NoCheck);
	ASSERT_FALSE(s);
	ASSERT_TRUE(!s);
}

TEST(Strong, xorAdd)
{
	Flags<Strong> s(Strong::NoLog | Strong::NoCheck);

	s ^= Strong::NoCheck;
	ASSERT_TRUE(s & Strong::NoLog);
	ASSERT_TRUE((s & Strong::NoLog) == Strong::NoLog);
	ASSERT_FALSE(s & Strong::NoCheck);
	ASSERT_FALSE((s & Strong::NoCheck) == Strong::NoCheck);
}

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

	return RUN_ALL_TESTS();
}