Path: blob/master/test/hotspot/gtest/utilities/test_tribool.cpp
41144 views
/*1* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "unittest.hpp"25#include "utilities/tribool.hpp"2627TEST(tribool, TriBool) {28TriBool t1;29ASSERT_EQ(t1.is_default(), true);30ASSERT_EQ((bool)t1, false);3132TriBool t2(false);33ASSERT_TRUE(t2.is_default() == false && (bool)t2 == false);3435TriBool t3(true);36ASSERT_TRUE(t3.is_default() == false && (bool)t3 == true);3738TriBool t4 = false;39ASSERT_TRUE(t4.is_default() == false && (bool)t4 == false);4041if (t2 || !t3 || t4) {42ASSERT_TRUE(false); //boom43}4445TriBool flags[4];46flags[0] = TriBool();47flags[1] = false;48flags[2] = true;4950ASSERT_EQ(flags[0].is_default(), true) << "should be default";51ASSERT_EQ(!flags[1].is_default() && !flags[1], true) << "should be not default and not set";52ASSERT_EQ(!flags[2].is_default() && flags[2], true) << "should be not default and set";53ASSERT_EQ(flags[3].is_default() == true, true) << "should be default";54}5556template <size_t SZ, typename T>57struct Tester {58static void doit() {59// test fill_in(value)60control_words.fill_in(TriBool());61for (size_t i = 0; i < SZ; ++i) {62EXPECT_TRUE(control_words[i].is_default());63}6465TriBool F = false;66control_words.fill_in(F);67for (size_t i = 0; i < SZ; ++i) {68EXPECT_TRUE(!control_words[i].is_default() && control_words[i] == false);69}7071// test fill_in(beg, end)72TriBool Vec[4];73Vec[0] = TriBool();74Vec[1] = TriBool();75Vec[2] = true;76Vec[3] = false;7778control_words.fill_in(&Vec[0], Vec + 4);7980if (0 < SZ) {81EXPECT_TRUE(control_words[0].is_default());82}8384if (1 < SZ) {85EXPECT_TRUE(control_words[1].is_default());86}8788if (2 < SZ) {89EXPECT_TRUE(!control_words[2].is_default() && control_words[2] == true);90}9192if (3 < SZ) {93EXPECT_TRUE(!control_words[3].is_default() && control_words[3] == false);94}9596// test assignment97for (size_t i = 0; i < SZ; ++i) {98control_words[i] = true;99EXPECT_TRUE(!control_words[i].is_default() && control_words[i] == true);100}101102for (size_t i = 0; i < SZ; ++i) {103control_words[i] = false;104EXPECT_TRUE(!control_words[i].is_default() && control_words[i] == false);105}106107for (size_t i = 0; i < SZ; ++i) {108if ((i%2) == 0) {109control_words[i] = TriBool(true);110}111else {112control_words[i] = TriBool(false);113}114}115116// test copy constructor(default)117copy = control_words;118for (size_t i = 0; i < SZ; ++i) {119if ((i%2) == 0) {120EXPECT_TRUE(!copy[i].is_default() && copy[i] == true)121<< "even value must be true.";122}123else {124EXPECT_TRUE(!copy[i].is_default() && copy[i] == false)125<< "odd value must be false.";126}127}128129// test const operator[](fastpath)130const TriBoolArray<SZ, T>& cref = control_words;131for (size_t i = 0; i < SZ; ++i) {132if ((i%2) == 0) {133EXPECT_TRUE(!cref[i].is_default() && cref[i] == true)134<< "even value must be true.";135}136else {137EXPECT_TRUE(!cref[i].is_default() && cref[i] == false)138<< "odd value must be false.";139}140}141142EXPECT_GE(sizeof(control_words) * 8, (2 * SZ)) << "allocated too less";143EXPECT_LE(sizeof(control_words), (((2 * SZ) / (sizeof(T) * 8) + 1) * sizeof(T)))144<< "allocated too much";145}146147// because doit probably can't allocate jumbo arrays on stack, use static members148static TriBoolArray<SZ, T> control_words;149static TriBoolArray<SZ, T> copy;150};151152template<size_t SZ, typename T>153TriBoolArray<SZ, T> Tester<SZ, T>::control_words;154155template<size_t SZ, typename T>156TriBoolArray<SZ, T> Tester<SZ, T>::copy;157158TEST(tribool, TriBoolArray) {159Tester<1, int>::doit();160Tester<2, int>::doit();161Tester<3, int>::doit();162Tester<7, int>::doit();163Tester<8, int>::doit();164Tester<14, int>::doit();165Tester<16, int>::doit();166Tester<27, int>::doit();167Tester<32, int>::doit();168Tester<34, int>::doit();169Tester<81, int>::doit();170Tester<128, int>::doit();171Tester<328, int>::doit(); // the no of intrinsics in jdk15172173Tester<1024, int>::doit();174Tester<1025, int>::doit();175176Tester<4 <<10/*4k*/ , int>::doit();177Tester<16<<10/*16k*/, int>::doit();178Tester<32<<10/*32k*/, int>::doit();179Tester<1 <<20/*1M*/ , int>::doit();180Tester<4 <<20/*4M*/ , int>::doit();181}182183TriBool global_single;184TriBoolArray<2, unsigned int> global_tuple;185TEST(tribool, StaticInitializer) {186EXPECT_TRUE(global_single.is_default());187EXPECT_TRUE(global_tuple[0].is_default());188EXPECT_TRUE(global_tuple[1].is_default());189}190191192