From 5a124c2d865907852e6d0e7d22080517e470c700 Mon Sep 17 00:00:00 2001 From: Neil Natekar Date: Thu, 19 Mar 2020 22:18:26 -0700 Subject: [PATCH] tests, base: Added GTests for base/amo.hh Issue-on: https://gem5.atlassian.net/browse/GEM5-231 Change-Id: Ia915f9c8bd0732c6c918e8056253bd2fdcdf6b5d Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/26944 Reviewed-by: Daniel Carvalho Maintainer: Jason Lowe-Power Tested-by: kokoro --- src/base/SConscript | 1 + src/base/amo.test.cc | 245 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 src/base/amo.test.cc diff --git a/src/base/SConscript b/src/base/SConscript index e04d84a71..a83324ad4 100644 --- a/src/base/SConscript +++ b/src/base/SConscript @@ -29,6 +29,7 @@ Import('*') SimObject('Graphics.py') +GTest('amo.test', 'amo.test.cc') Source('atomicio.cc') GTest('atomicio.test', 'atomicio.test.cc', 'atomicio.cc') Source('bitfield.cc') diff --git a/src/base/amo.test.cc b/src/base/amo.test.cc new file mode 100644 index 000000000..e1a56c129 --- /dev/null +++ b/src/base/amo.test.cc @@ -0,0 +1,245 @@ +/* + * Copyright (c) 2020 The Regents of the University of California + * All rights reserved + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "base/amo.hh" + +void +multiply2Op(int *b, int a) +{ + *b *= a; +} + +void +multiply3Op(int *b, int a, int c) +{ + *b *= a * c; +} + +void +addSubColumns(int *b, const std::array& a, const std::array& c) +{ + *b += a[0] + c[0]; + *b -= a[1] + c[1]; +} + +TEST(AmoTest, AtomicOpMin) +{ + // test with ints and strings + int test_int_smaller = 5; + int test_int_bigger = 15; + std::string test_string_smaller = "apple"; + std::string test_string_bigger = "cat"; + + TypedAtomicOpFunctor *amo_op_int = new AtomicOpMin(10); + TypedAtomicOpFunctor *amo_op_string = + new AtomicOpMin("base"); + amo_op_int->execute(&test_int_smaller); + amo_op_int->execute(&test_int_bigger); + amo_op_string->execute(&test_string_smaller); + amo_op_string->execute(&test_string_bigger); + + EXPECT_EQ(test_int_smaller, 5); + EXPECT_EQ(test_int_bigger, 10); + EXPECT_EQ(test_string_smaller, "apple"); + EXPECT_EQ(test_string_bigger, "base"); +} + +TEST(AmoTest, AtomicOpMax) +{ + int test_int_smaller = 5; + int test_int_bigger = 15; + std::string test_string_smaller = "apple"; + std::string test_string_bigger = "cat"; + + TypedAtomicOpFunctor *amo_op_int = new AtomicOpMax(10); + TypedAtomicOpFunctor *amo_op_string = + new AtomicOpMax("base"); + amo_op_int->execute(&test_int_smaller); + amo_op_int->execute(&test_int_bigger); + amo_op_string->execute(&test_string_smaller); + amo_op_string->execute(&test_string_bigger); + + EXPECT_EQ(test_int_smaller, 10); + EXPECT_EQ(test_int_bigger, 15); + EXPECT_EQ(test_string_smaller, "base"); + EXPECT_EQ(test_string_bigger, "cat"); +} + +TEST(AmoTest, AtomicOpDec) +{ + int test_int = 10; + char test_char = 'c'; + + TypedAtomicOpFunctor *amo_op_int = new AtomicOpDec(); + TypedAtomicOpFunctor *amo_op_char = new AtomicOpDec(); + amo_op_int->execute(&test_int); + amo_op_char->execute(&test_char); + + EXPECT_EQ(test_int, 9); + EXPECT_EQ(test_char, 'b'); +} + +TEST(AmoTest, AtomicOpInc) +{ + int test_int = 10; + char test_char = 'c'; + + TypedAtomicOpFunctor *amo_op_int = new AtomicOpInc(); + TypedAtomicOpFunctor *amo_op_char = new AtomicOpInc(); + amo_op_int->execute(&test_int); + amo_op_char->execute(&test_char); + + EXPECT_EQ(test_int, 11); + EXPECT_EQ(test_char, 'd'); +} + +TEST(AmoTest, AtomicOpSub) +{ + int test_int = 10; + char test_char = 'c'; + + TypedAtomicOpFunctor *amo_op_int = new AtomicOpSub(2); + TypedAtomicOpFunctor *amo_op_char = new AtomicOpSub('a'); + amo_op_int->execute(&test_int); + amo_op_char->execute(&test_char); + + EXPECT_EQ(test_int, 8); + EXPECT_EQ(test_char, 2); +} + +TEST(AmoTest, AtomicOpAdd) +{ + int test_int = 10; + char test_char = 'c'; + + TypedAtomicOpFunctor *amo_op_int = new AtomicOpAdd(2); + TypedAtomicOpFunctor *amo_op_char = new AtomicOpAdd(2); + amo_op_int->execute(&test_int); + amo_op_char->execute(&test_char); + + EXPECT_EQ(test_int, 12); + EXPECT_EQ(test_char, 'e'); +} + +TEST(AmoTest, AtomicOpExch) +{ + int test_int = 10; + char test_char = 'c'; + + TypedAtomicOpFunctor *amo_op_int = new AtomicOpExch(2); + TypedAtomicOpFunctor *amo_op_char = new AtomicOpExch('a'); + amo_op_int->execute(&test_int); + amo_op_char->execute(&test_char); + + EXPECT_EQ(test_int, 2); + EXPECT_EQ(test_char, 'a'); +} + +TEST(AmoTest, AtomicOpXor) +{ + int test_int = 10; + char test_char = 'c'; + + TypedAtomicOpFunctor *amo_op_int = new AtomicOpXor(2); + TypedAtomicOpFunctor *amo_op_char = new AtomicOpXor('a'); + amo_op_int->execute(&test_int); + amo_op_char->execute(&test_char); + + EXPECT_EQ(test_int, 8); // 1010 ^ 0010 = 1000 + EXPECT_EQ(test_char, 2); // 99 ^ 97 = 2 +} + +TEST(AmoTest, AtomicOpOr) +{ + int test_int = 8; + bool test_bool = true; + + TypedAtomicOpFunctor *amo_op_int = new AtomicOpOr(2); + TypedAtomicOpFunctor *amo_op_bool = new AtomicOpOr(false); + amo_op_int->execute(&test_int); + amo_op_bool->execute(&test_bool); + + EXPECT_EQ(test_int, 10); + EXPECT_EQ(test_bool, true); +} + +TEST(AmoTest, AtomicOpAnd) +{ + int test_int = 10; + char test_char = 'c'; + + TypedAtomicOpFunctor *amo_op_int = new AtomicOpAnd(6); + TypedAtomicOpFunctor *amo_op_char = new AtomicOpAnd('a'); + amo_op_int->execute(&test_int); + amo_op_char->execute(&test_char); + + EXPECT_EQ(test_int, 2); + EXPECT_EQ(test_char, 'a'); +} + +TEST(AmoTest, AtomicGeneric2Op) +{ + int test_int = 9; + + TypedAtomicOpFunctor *amo_op_int = + new AtomicGeneric2Op(9, multiply2Op); + amo_op_int->execute(&test_int); + + EXPECT_EQ(test_int, 81); +} + +TEST(AmoTest, AtomicGeneric3Op) +{ + int test_int = 2; + + TypedAtomicOpFunctor *amo_op_int = + new AtomicGeneric3Op(4, 3, multiply3Op); + amo_op_int->execute(&test_int); + + EXPECT_EQ(test_int, 24); +} + +TEST(AmoTest, AtomicGenericPair3Op) +{ + int test_int = 5; + + std::array a = {6, 3}; + std::array c = {10, 8}; + TypedAtomicOpFunctor *amo_op_int = + new AtomicGenericPair3Op(a, c, addSubColumns); + amo_op_int->execute(&test_int); + + EXPECT_EQ(test_int, 10); +} -- 2.30.2