Search code examples
c++googlemock

gmock ElementsAreArray() Matcher gives compiler errors in gmock-matchers.h


I'm working on some tests so that I can refactor an old C++ project. I'm trying to match two arrays using the gmock matcher ElementsAreArray().

EXPECT_THAT(value_instance.value, ::testing::ElementsAreArray(var_array));

where value_instance.value is a pointer to a C array.

However when I compile with this line of code in my test I get the following error output from the gmock-matchers.h file:

Error   1   error C2510: 'type' : left of '::' must be a class/struct/union s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2535    1   Project_Tests
Error   2   error C2146: syntax error : missing ';' before identifier 'Element' s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2536    1   Project_Tests
Error   3   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2536    1   Project_Tests
Error   4   error C2065: 'Element' : undeclared identifier  s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2536    1   Project_Tests
Error   5   error C2825: 'testing::internal::ElementsAreMatcherImpl<Container>::StlContainer': must be a class or namespace when followed by '::'   s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2399    1   Project_Tests
Error   6   error C2039: 'value_type' : is not a member of '`global namespace'' s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2399    1   Project_Tests
Error   7   error C2146: syntax error : missing ';' before identifier 'Element' s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2399    1   Project_Tests
Error   8   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2399    1   Project_Tests
Error   9   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2502    1   Project_Tests
Error   10  error C2146: syntax error : missing ',' before identifier 'Element' s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2502    1   Project_Tests
Error   11  error C2065: 'Element' : undeclared identifier  s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2502    1   Project_Tests
Error   12  error C2059: syntax error : '>' s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2502    1   Project_Tests
Error   13  error C2143: syntax error : missing ';' before '}'  s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2506    1   Project_Tests
Error   14  error C1004: unexpected end-of-file found   s:\repo_file\gmock-1.6.0\include\gmock\gmock-matchers.h 2506    1   Project_Tests

I'm probably missing something really silly but I can't seem to find the problem.

#include <cstring>    
#include "gtest/gtest.h"
#include "gmock/gmock.h"

//This is a stripped down version of The Class under test

enum {
    VAL_TYPE_UNKNOWN, VAL_TYPE_INT, VAL_TYPE_BOOL, VAL_TYPE_ARRAY
};      

class ClassUnderTest
{
  public:

    int alloc_len;
    int len;
    int type;
    long i_value;
    unsigned char *value;    

    ClassUnderTest (void)
    {
      alloc_len = 0;
      len = 0;
      value = 0;
      i_value = 0;
      type = VAL_TYPE_INT;
    }  

    ~ClassUnderTest (void)
    {
      if (value)
      {
        delete [] value;
        value = 0;
      }
    }              

    void Init (void *val,  int v_len)
    {
      NewLength(v_len);
      if (val)
        memcpy(value, val, v_len);
      type = VAL_TYPE_ARRAY;
    }  

    void NewLength (int new_len)
    {
      unsigned char *old;

      if ((new_len > alloc_len) || (new_len == 0))
      {
        old = value;
        value = 0;
        if (new_len > 0)
        {
          value = new unsigned char [new_len];
          memset(value, 0, new_len);
        }
        alloc_len = new_len;
        if (old)
        {
          if (value)
            memcpy(value, old, len);
          delete [] old;
        }
      }
      else if (new_len > len)
        memset(value+len, 0, new_len-len);
      len = new_len;
    }                                         
}                      

//this fails with the error list above

TEST(ClassUnderTestTests, TestInit)
{
  ClassUnderTest value_instance;
  unsigned char var_array[] = {1, 2, 3, 4, 5};
  value_instance.Init((void *)var_array, sizeof(var_array));
  EXPECT_EQ(value_instance.len, sizeof(var_array));
  EXPECT_EQ(value_instance.i_value, 0);
  EXPECT_EQ(value_instance.type, VAL_TYPE_ARRAY);
  EXPECT_THAT(value_instance.value, testing::ElementsAreArray(var_array));
}                            



//This example works However the code above does not

TEST(ClassUnderTestTests, ElementsAreArrayFailure)
{
  int array1[] = {1, 2, 3, 4, 5};
  int array2[] = {1, 2, 3, 4, 5};
  EXPECT_THAT(array1, testing::ElementsAreArray(array2));
}                                    

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

Solution

  • You need to dereference the pointer to the C-array.

    EXPECT_THAT(*value_instance.value, ::testing::ElementsAreArray(var_array));
    //          ^--- dereference the C-array
    

    Update:

    OK, having seen your code now, I'd say that value_instance.value is a dynamically-allocated array rather than a pointer to a C array (see comp.lang.c FAQ on Arrays and Pointers).

    So the simple fix is to swap the arguments in the EXPECT_THAT(value, matcher) macro. The value can't just be a pointer in this case, you need to give it a std::-like container (e.g. a C array). However, the ElementsAreArray function can handle being passed a dynamically-allocated array, assuming you also pass it the size of the array (since it can't be deduced).

    EXPECT_THAT(var_array,
                testing::ElementsAreArray(value_instance.value, value_instance.len));
    

    Rather than swapping the order of the arguments to EXPECT_THAT, you could alternatively construct a temporary vector from value_instance.value and pass that as the value:

    std::vector<unsigned char> value_copy(value_instance.value,
                                          value_instance.value + value_instance.len);
    EXPECT_THAT(value_copy, testing::ElementsAreArray(var_array));