Added a GUID test suite

The plan is to change gnc guid to use boost's guid implementation. There were
no tests for guid before, but we need them to ensure that we don't have regressions
when that work begins.
This commit is contained in:
lmat
2014-05-02 09:50:22 -04:00
committed by John Ralls
parent ce409753b8
commit 949702d04d
3 changed files with 164 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ test_qof_SOURCES = \
test-qofobject.c \
test-qofsession.c \
test-qof-string-cache.c \
test-gnc-guid.cpp \
${top_srcdir}/src/test-core/unittest-support.c
test_qof_HEADERS = \
@@ -38,7 +39,7 @@ test_qof_LDADD = \
${top_builddir}/${MODULEPATH}/libgnc-qof.la \
$(GLIB_LIBS)
test_qof_CFLAGS = \
test_qof_CPPFLAGS = \
${DEFAULT_INCLUDES} \
-I$(top_srcdir)/${MODULEPATH} \
-I$(top_srcdir)/src/test-core \

View File

@@ -0,0 +1,160 @@
/********************************************************************
* GLib test suite for guid.cpp *
* Copyright 2014 Aaron Laws <dartmetrash@gmail.com> *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, you can retrieve it from *
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html *
* or contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
********************************************************************/
extern "C"
{
#include <config.h>
#include <string.h>
#include <glib.h>
#include <unittest-support.h>
void test_suite_gnc_guid (void);
}
/*Can be included as c++, because it's c++ tolerant*/
#include "../guid.h"
#include <random>
#include <sstream>
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;
static const gchar * suitename {"/qof/gnc-guid"};
/*Create a GUID and free it.*/
static void test_create_gnc_guid (void){
GncGUID * guid {guid_malloc ()};
g_assert (guid != nullptr);
guid_new (guid);
/*We apparently don't need to free guid_null (based on its being const)*/
const GncGUID * guidnull {guid_null ()};
g_assert (!guid_equal (guid, guidnull));
guid_free (guid);
}
/*We create a GUID, create a copy, and compare them to ensure they're not different.*/
static void test_gnc_guid_copy (void) {
GncGUID * guid {guid_malloc ()};
g_assert (guid != nullptr);
guid_new (guid);
GncGUID * cp {guid_copy (guid)};
g_assert (guid_equal (guid, cp));
guid_free (cp);
guid_free (guid);
}
/* We create a GUID, then convert it to a string using the two methods
defined in the guid api. We then compare them.*/
static void test_gnc_guid_to_string (void) {
GncGUID * guid {guid_malloc()};
g_assert (guid != nullptr);
guid_new (guid);
string message {" using guid_to_string (deprecated): "};
/*don't free the return value of guid_to_string!*/
string guidstr {guid_to_string (guid)};
g_assert (guidstr.size () == GUID_ENCODING_LENGTH);
message += guidstr;
g_test_message ("%s", message.c_str ());
message = " using guid_to_string_buff: ";
gchar guidstrp2 [GUID_ENCODING_LENGTH+1];
gchar * ret {guid_to_string_buff (guid, guidstrp2)};
g_assert (ret == guidstrp2 + GUID_ENCODING_LENGTH);
string guidstr2 {guidstrp2};
g_assert (guidstr2.size () == GUID_ENCODING_LENGTH);
message += guidstr2;
g_test_message ("%s", message.c_str ());
g_assert (guidstr2 == guidstr);
guid_free (guid);
}
/*We fill a stringstream with random data, convert those to
two GUIDs, then ensure that they are equal*/
static void test_gnc_guid_equals (void) {
GncGUID * guid1 {guid_malloc ()};
g_assert (guid1 != nullptr);
GncGUID * guid2 {guid_malloc ()};
g_assert (guid2 != nullptr);
GncGUID * guidold {guid_malloc ()};
g_assert (guidold != nullptr);
mt19937 m;
uniform_int_distribution<unsigned> dist;
/*we use the same seed every time for reproducibility's sake, and
for sanity's sake. It may be nerve-wracking for this test to fail
only sometimes, or in different ways.*/
m.seed (0);
unsigned num_tests {200};
for (unsigned test {0}; test < num_tests; ++test){
ostringstream o;
o << hex << setfill ('0') << right;
/*Now we put a GUID into the stringstream*/
for (unsigned spot {0}; spot < 4; ++spot)
o << setw (8) << dist (m);
string guids {o.str ()};
g_assert (guids.size() == GUID_ENCODING_LENGTH);
g_assert (string_to_guid (guids.c_str (), guid1));
g_assert (string_to_guid (guids.c_str (), guid2));
g_assert (guid_equal (guid1, guid2));
/*Assuming that our distribution won't give the same
GUID twice in a row.*/
g_assert (!guid_equal (guid1, guidold));
g_assert (string_to_guid (guids.c_str (), guidold));
}
guid_free (guidold);
guid_free (guid2);
guid_free (guid1);
}
/*We create a new guid, convert it to string, and convert that to
a guid, ensuring that we end up with an equivalent structure*/
static void test_gnc_guid_roundtrip (void) {
GncGUID * guid1 {guid_malloc ()};
g_assert (guid1 != nullptr);
GncGUID * guid2 {guid_malloc ()};
g_assert (guid2 != nullptr);
guid_new (guid1);
gchar guidstrp [GUID_ENCODING_LENGTH+1];
gchar * temp {guid_to_string_buff (guid1, guidstrp)};
g_assert (temp == guidstrp + GUID_ENCODING_LENGTH);
g_assert (string_to_guid (guidstrp, guid2));
g_assert (guid_equal (guid1, guid2));
guid_free (guid2);
guid_free (guid1);
}
void test_suite_gnc_guid (void)
{
guid_init ();
GNC_TEST_ADD_FUNC (suitename, "gnc create guid", test_create_gnc_guid);
GNC_TEST_ADD_FUNC (suitename, "gnc copy guid", test_gnc_guid_copy);
GNC_TEST_ADD_FUNC (suitename, "gnc guid to string", test_gnc_guid_to_string);
GNC_TEST_ADD_FUNC (suitename, "gnc guid equal", test_gnc_guid_equals);
GNC_TEST_ADD_FUNC (suitename, "gnc guid string roundtrip", test_gnc_guid_roundtrip);
guid_shutdown ();
}

View File

@@ -32,6 +32,7 @@ extern void test_suite_qofobject();
extern void test_suite_qofsession();
extern void test_suite_gnc_date();
extern void test_suite_qof_string_cache();
extern void test_suite_gnc_guid ( void );
int
main (int argc,
@@ -43,6 +44,7 @@ main (int argc,
// g_log_set_always_fatal (0);
g_test_bug_base("https://bugzilla.gnome.org/show_bug.cgi?id="); /* init the bugzilla URL */
test_suite_gnc_guid();
test_suite_qofbook();
test_suite_qofinstance();
test_suite_kvp_frame();