この公式ドキュメントを読んで、バイナリ比較と文字列比較の方法を学びました。
ASSERT_EQ と ASSERT_STREQ は、配列比較の場合に機能しませんでした。
例えば
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
insert_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();
}
insert_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;
}
}
}
}
insertsort.h
#ifndef INSERTIONSORT_H_
#define INSERTIONSORT_H_
void insertionSort(int *data, int size);
#endif