Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does gtest compare the values in two arrays?

Tags:

c++

googletest

I have read this official document, learn how to do Binary Comparison and String Comparison.

The ASSERT_EQ and ASSERT_STREQ could not work in the array comparison case.

For example

li@li:~/poc$ g++ -I${GTEST_DIR}/include insertion_sort.cpp insertion_sort_unittest.cpp /home/li/libgtest.a -lpthread -o inser_unit
li@li:~/poc$ ./inser_unit 
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from InsertionSortTest
[ RUN      ] InsertionSortTest.Two
insertion_sort_unittest.cpp:18: Failure
Value of: two_sorted
  Actual: { 2, 5 }
Expected: two
Which is: { 2, 5 }
[  FAILED  ] InsertionSortTest.Two (1 ms)
[----------] 1 test from InsertionSortTest (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] InsertionSortTest.Two

 1 FAILED TEST

insertion_sort_unittest.cpp

#include <limits.h>
#include "insertionsort.h"
#include "gtest/gtest.h"

namespace{
    class InsertionSortTest : public ::testing::Test{
        protected:
            InsertionSortTest() {}
            virtual ~InsertionSortTest() {}
            virtual void SetUp() {}
            virtual void TearDown() {}
    };

    TEST(InsertionSortTest, Two){
        int two[] = {5, 2};
        int two_sorted[] = {2, 5};
        insertionSort(two, 2);
        EXPECT_EQ(two, two_sorted);
    }
}

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

insertion_sort.cpp

#include "insertionsort.h"
void insertionSort(int *data, int size){
    for (int i=1,j; i<size; i++){
        int key = data[i];
        for (j=i-1; j>=0; j--){
            if (data[j] > key){
                data[j+1]=data[j];
                data[j]=key;
            }
        }
    }
}

insertionsort.h

#ifndef INSERTIONSORT_H_
#define INSERTIONSORT_H_
void insertionSort(int *data, int size);
#endif
like image 378
hellotli Avatar asked Apr 08 '12 03:04

hellotli


People also ask

What is Gtest used for?

It is a test framework i.e., a software tool for writing and running unit tests. It is a library for writing C++ tests. It is based on xUnit architecture which is a set of “Frameworks” for programming and automated execution of test cases.

Does Gtest include gMock?

gMock is bundled with googletest.

What is Test_f in Gtest?

TEST_F(TestFixtureName, TestName) { ... statements ... } Defines an individual test named TestName that uses the test fixture class TestFixtureName . The test suite name is TestFixtureName .

Does Google Test run tests in parallel?

gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).


1 Answers

You don't need to add a dependency on googlemock if you don't want, you could write your own simple function that returns a testing::AssertionResult, e.g.

    template<typename T, size_t size>
    ::testing::AssertionResult ArraysMatch(const T (&expected)[size],
                                           const T (&actual)[size]){
        for (size_t i(0); i < size; ++i){
            if (expected[i] != actual[i]){
                return ::testing::AssertionFailure() << "array[" << i
                    << "] (" << actual[i] << ") != expected[" << i
                    << "] (" << expected[i] << ")";
            }
        }

        return ::testing::AssertionSuccess();
    }

Then in your test, call:

    EXPECT_TRUE(ArraysMatch(two_sorted, two));
like image 55
Fraser Avatar answered Nov 14 '22 00:11

Fraser