2009-09-22 16 views

Respuesta

72

Realmente recomendaría mirar Google C++ Mocking Framework. Incluso si no quiere burlarse de nada, le permite escribir afirmaciones bastante complicadas con facilidad.

Por ejemplo

//checks that vector v is {5, 10, 15} 
ASSERT_THAT(v, ElementsAre(5, 10, 15)); 

//checks that map m only have elements 1 => 10, 2 => 20 
ASSERT_THAT(m, ElementsAre(Pair(1, 10), Pair(2, 20))); 

//checks that in vector v all the elements are greater than 10 and less than 20 
ASSERT_THAT(v, Each(AllOf(Gt(10), Lt(20)))); 

//checks that vector v consist of 
// 5, number greater than 10, anything. 
ASSERT_THAT(v, ElementsAre(5, Gt(10), _)); 

Hay un montón de matchers para las situaciones de todo, y se pueden combinar para lograr casi cualquier cosa.

¿Te he dicho que ElementsAre solo necesita iterators y size() método en una clase para trabajar? Por lo tanto, no solo funciona con ningún contenedor de STL sino también con contenedores personalizados.

Google Mock afirma ser casi tan portátil como Google Test y, francamente, no veo por qué no lo usaría. Es simplemente puramente increíble.

+6

Utilizo Google Mock. Y acepto que es increíble. Nunca esperé ver algo así para C++. –

+1

"puramente increíble" jajaja Voy a probarlo también –

+2

'ElementsAreArray' es mejor comparar matrices, ya que' ElementsAre' tiene un límite de 10 elementos. –

9

Tenía exactamente la misma pregunta, así que escribí un par de macros que hacen comparaciones entre dos contenedores genéricos. Es extensible a CUALQUIER contenedor que tenga const_iterator, begin y end. Si falla, mostrará un mensaje detallado de dónde salió mal la matriz y lo hará para cada elemento que falle; se asegurará de que tengan la misma longitud; y la ubicación en su código que informa que falla es la misma línea donde llama al EXPECT_ITERABLE_EQ(std::vector<double>, a, b).

//! Using the google test framework, check all elements of two containers 
#define EXPECT_ITERABLE_BASE(PREDICATE, REFTYPE, TARTYPE, ref, target) \ 
    { \ 
    const REFTYPE& ref_(ref); \ 
    const TARTYPE& target_(target); \ 
    REFTYPE::const_iterator refIter = ref_.begin(); \ 
    TARTYPE::const_iterator tarIter = target_.begin(); \ 
    unsigned int i = 0; \ 
    while(refIter != ref_.end()) { \ 
     if (tarIter == target_.end()) { \ 
      ADD_FAILURE() << #target " has a smaller length than " #ref ; \ 
      break; \ 
     } \ 
     PREDICATE(* refIter, * tarIter) \ 
      << "Containers " #ref " (refIter) and " #target " (tarIter)" \ 
       " differ at index " << i; \ 
     ++refIter; ++tarIter; ++i; \ 
    } \ 
    EXPECT_TRUE(tarIter == target_.end()) \ 
     << #ref " has a smaller length than " #target ; \ 
    } 

//! Check that all elements of two same-type containers are equal 
#define EXPECT_ITERABLE_EQ(TYPE, ref, target) \ 
    EXPECT_ITERABLE_BASE(EXPECT_EQ, TYPE, TYPE, ref, target) 

//! Check that all elements of two different-type containers are equal 
#define EXPECT_ITERABLE_EQ2(REFTYPE, TARTYPE, ref, target) \ 
    EXPECT_ITERABLE_BASE(EXPECT_EQ, REFTYPE, TARTYPE, ref, target) 

//! Check that all elements of two same-type containers of doubles are equal 
#define EXPECT_ITERABLE_DOUBLE_EQ(TYPE, ref, target) \ 
    EXPECT_ITERABLE_BASE(EXPECT_DOUBLE_EQ, TYPE, TYPE, ref, target) 

Espero que esto funcione para usted (y que realmente compruebe esta respuesta dos meses después de haber enviado su pregunta).

+0

Eso es un gran acercamiento! ¿Tal vez podría proporcionar esto a Google para que lo agreguen al marco? –

+1

Dijeron (http://code.google.com/p/googletest/issues/detail?id=231) que no recomiendan agregar macros, y esta funcionalidad está disponible hasta cierto punto en el marco de trabajo de Google Mock. –

4

Me encontré con un problema similar al comparar matrices en google test.

Ya que necesitaba comparación con void* básica y char* (para las pruebas de código de bajo nivel), yo no lo sea Google simulacro (que también estoy usando en el proyecto) o un gran macro de Seth me podía ayudar en la situación particular. Escribí la siguiente macro:

#define EXPECT_ARRAY_EQ(TARTYPE, reference, actual, element_count) \ 
    {\ 
    TARTYPE* reference_ = static_cast<TARTYPE *> (reference); \ 
    TARTYPE* actual_ = static_cast<TARTYPE *> (actual); \ 
    for(int cmp_i = 0; cmp_i < element_count; cmp_i++){\ 
     EXPECT_EQ(reference_[cmp_i], actual_[cmp_i]);\ 
    }\ 
    } 

Los moldes están ahí para hacer la macro se puede utilizar cuando la comparación void* a otras cosas:

void* retrieved = ptr->getData(); 
    EXPECT_EQ(6, ptr->getSize()); 
    EXPECT_ARRAY_EQ(char, "data53", retrieved, 6) 

Tobias en los comentarios sugirieron fundición void* a char* y el uso de EXPECT_STREQ, una macro que de alguna manera me perdí antes, que parece una mejor alternativa.

+2

Preferiría convertir el vacío * en un char * y utilizando EXPECT_STREQ. ¿No funcionaría eso también? –

+0

Una de las razones por las que publiqué mi respuesta fue porque esperaba que alguien sugiriera una mejor alternativa. Parece que lo hiciste, Tobias :) – nietaki

+0

Me alegro de poder ayudar :) –

3

A continuación se muestra una afirmación que escribí para comparar [fragmentos de] dos matrices de punto flotante:

/* See 
http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ 
for thorough information about comparing floating point values. 
For this particular application we know that the value range is -1 to 1 (audio signal), 
so we can compare to absolute delta of 1/2^22 which is the smallest representable value in 
a 22-bit recording. 
*/ 
const float FLOAT_INEQUALITY_TOLERANCE = float(1.0/(1 << 22)); 


template <class T> 
::testing::AssertionResult AreFloatingPointArraysEqual(
           const T* const expected, 
           const T* const actual, 
           unsigned long length) 
{ 
    ::testing::AssertionResult result = ::testing::AssertionFailure(); 
    int errorsFound = 0; 
    const char* separator = " "; 
    for (unsigned long index = 0; index < length; index++) 
    { 
     if (fabs(expected[index] - actual[index]) > FLOAT_INEQUALITY_TOLERANCE) 
     { 
      if (errorsFound == 0) 
      { 
       result << "Differences found:"; 
      } 
      if (errorsFound < 3) 
      { 
       result << separator 
         << expected[index] << " != " << actual[index] 
         << " @ " << index; 
       separator = ", "; 
      } 
      errorsFound++; 
     } 
    } 
    if (errorsFound > 0) 
    { 
     result << separator << errorsFound << " differences in total"; 
     return result; 
    } 
    return ::testing::AssertionSuccess(); 
} 

uso dentro del marco de exámenes de Google es la siguiente:

EXPECT_TRUE(AreFloatingPointArraysEqual(expectedArray, actualArray, lengthToCompare)); 

En caso de error, se produce algo así como la siguiente salida:

..\MyLibraryTestMain.cpp:145: Failure 
Value of: AreFloatingPointArraysEqual(expectedArray, actualArray, lengthToCompare) 
    Actual: false (Differences found: 0.86119759082794189 != 0.86119747161865234 @ 14, -0.5552707314491272 != -0.55527061223983765 @ 24, 0.047732405364513397 != 0.04773232713341713 @ 36, 339 differences in total) 
Expected: true 

Para una discusión exhaustiva sobre la comparación de floati ng valores de punto en general, consulte this.

12

Si sólo tiene que comprobar si los arreglos son iguales, entonces la fuerza bruta también funciona:

int arr1[10]; 
int arr2[10]; 

// initialize arr1 and arr2 

EXPECT_TRUE(0 == std::memcmp(arr1, arr2, sizeof(arr1))); 

Sin embargo, esto no te dice qué elemento es diferente.

+3

Bueno, conocer la diferencia es una especie de punto. – filmil

11

Si desea comparar un puntero de matriz c-style a una matriz con Google Mock, puede ir a std :: vector. Por ejemplo: ElementsAreArray de

uint8_t expect[] = {1, 2, 3, 42}; 
uint8_t * buffer = expect; 
uint32_t buffer_size = sizeof(expect)/sizeof(expect[0]); 
ASSERT_THAT(std::vector<uint8_t>(buffer, buffer + buffer_size), 
      ::testing::ElementsAreArray(expect)); 

Google Mock también acepta puntero y longitud que permiten la comparación de los dos punteros de la matriz c-estilo. Por ejemplo:

ASSERT_THAT(std::vector<uint8_t>(buffer, buffer + buffer_size), 
      ::testing::ElementsAreArray(buffer, buffer_size)); 

Pasé demasiado tiempo intentando unir esto. Gracias a this StackOverlow post para el recordatorio en std :: vector iterator initialization. Tenga en cuenta que este método copiará los elementos de la matriz de almacenamiento intermedio en std :: vector antes de la comparación.

+0

¡Eres mi héroe! – slaadvak

2

Utilicé un ciclo clásico en todos los elementos. Puede usar SCOPED_TRACE para leer en qué iteración difieren los elementos de la matriz. Esto le proporciona información adicional en comparación con algunos otros enfoques y es fácil de leer.

for (int idx=0; idx<ui16DataSize; idx++) 
{ 
    SCOPED_TRACE(idx); //write to the console in which iteration the error occurred 
    ASSERT_EQ(array1[idx],array2[idx]); 
} 
4
ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length"; 

for (int i = 0; i < x.size(); ++i) { 
    EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i; 
} 

Source

+0

Me gusta algo así. No requiere copiar los datos a un contenedor stl, y es bastante simple. Envolviendo esto en una macro para un tipo común de comparación de matriz (como un vector o matriz), simplemente se hace y hace el trabajo. – johnb003

Cuestiones relacionadas