Skip to content

Instantly share code, notes, and snippets.

@jralls
Created July 21, 2019 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jralls/12eb12adf9535cb47847cd901a9d324d to your computer and use it in GitHub Desktop.
Save jralls/12eb12adf9535cb47847cd901a9d324d to your computer and use it in GitHub Desktop.
Sample SWIG unit test source and sink functions
test_engine_swig.h:
AccountValueList* test_account_value_list_source();
gboolean test_account_value_list_sink();
test_engine_swig.c:
AccountValueList*
test_account_value_list_source()
{
AccountValueList *avl = NULL;
QofBook* book = qof_book_new();
Account* account_1 = xaccAccountMalloc(book);
Account* account_2 = xaccAccountMalloc(book);
Account* account_3 = xaccAccountMalloc(book);
gnc_numeric value_1 = gnc_numeric_create(594673, 100);
gnc_numeric value_2 = gnc_numeric_create(129467, 100);
gnc_numeric value_3 = gnc_numeric_create(894319, 100);
xaccAccountSetType(account_1, ACCOUNT_TYPE_ASSET);
xaccAccountSetName(account_1, "First");
xaccAccountSetType(account_2, ACCOUNT_TYPE_EQUITY);
xaccAccountSetName(account_2, "Second");
xaccAccountSetType(account_3, ACCOUNT_TYPE_LIABILITY);
xaccAccountSetName(account_3, "Third");
avl = gncAccountValueAdd(avl, account_1, value_1);
avl = gncAccountValueAdd(avl, account_1, value_1);
avl = gncAccountValueAdd(avl, account_1, value_1);
return avl;
}
gboolean
test_account_value_list_sink(AccountValueList* avl)
{
GncAccountValue* gav = NULL;
QofBook* book = NULL;
gnc_numeric value_1 = gnc_numeric_create(594673, 100);
gnc_numeric value_2 = gnc_numeric_create(129467, 100);
gnc_numeric value_3 = gnc_numeric_create(894319, 100);
gboolean pass = TRUE;
g_return_val_if_fail(avl && G_IS_LIST(avl), FALSE);
if (g_list_length(avl) != 3)
{
PWARN("List wrong length, expected 3, got %d", g_list_length(avl));
pass = FALSE;
}
gav = avl->data;
if (GNC_IS_ACCOUNT(gav->account))
book = qof_instance_get_book(QOF_INSTANCE(first_gav->account));
else
{
/* Abort, we can't safely do more */
PWARN("Account List pointers don't point to accounts");
return FALSE;
}
if (g_strcmp0(xaccAccountGetName(gav->account), "First") != 0)
{
PWARN("Wrong name %s for First account",
xaccAccountGetName(gav->account));
pass = FALSE;
}
if (xaccAccountGetType(gav->account) != ACCOUNT_TYPE_ASSET)
{
PWARN("First Account Type wrong, expected %d got %d",
ACCOUNT_TYPE_ASSET, xaccAccountGetType(gav->account));
pass = FALSE;
}
if (gnc_numeric_compare(value_1, gav->value) != 0)
{
PWARN("First Account value wrong, expected %d/%d got %d/%d",
gnc_numeric_num(value_1), gnc_numeric_den(value_1),
gnc_numeric_num(gav->value), gnc_numeric_den(gav->value));
pass = FALSE;
}
xaccAccountDestroy(gav->account);
gav->account = NULL;
avl = g_list_next(avl);
if (!avl)
{
PWARN("No second entry");
return FALSE;
}
if (g_strcmp0(xaccAccountGetName(gav->account), "First") != 0)
{
PWARN("Wrong name %s for First account",
xaccAccountGetName(gav->account));
pass = FALSE;
}
if (xaccAccountGetType(gav->account) != ACCOUNT_TYPE_ASSET)
{
PWARN("First Account Type wrong, expected %d got %d",
ACCOUNT_TYPE_ASSET, xaccAccountGetType(gav->account));
pass = FALSE;
}
if (gnc_numeric_compare(value_1, gav->value) != 0)
{
PWARN("First Account value wrong, expected %d/%d got %d/%d",
gnc_numeric_num(value_1), gnc_numeric_den(value_1),
gnc_numeric_num(gav->value), gnc_numeric_den(gav->value));
pass = FALSE;
}
xaccAccountDestroy(gav->account);
gav->account = NULL;
avl = g_list_next(avl);
if (!avl)
{
PWARN("No second entry");
return FALSE;
}
if (g_strcmp0(xaccAccountGetName(gav->account), "Second") != 0)
{
PWARN("Wrong name %s for Second account",
xaccAccountGetName(gav->account));
pass = FALSE;
}
if (xaccAccountGetType(gav->account) != ACCOUNT_TYPE_EQUITY)
{
PWARN("Second Account Type wrong, expected %d got %d",
ACCOUNT_TYPE_EQUITY, xaccAccountGetType(gav->account));
pass = FALSE;
}
if (gnc_numeric_compare(value_2, gav->value) != 0)
{
PWARN("Second Account value wrong, expected %d/%d got %d/%d",
gnc_numeric_num(value_2), gnc_numeric_den(value_2),
gnc_numeric_num(gav->value), gnc_numeric_den(gav->value));
pass = FALSE;
}
xaccAccountDestroy(gav->account);
gav->account = NULL;
avl = g_list_next(avl);
if (!avl)
{
PWARN("No third entry");
return FALSE;
}
if (g_strcmp0(xaccAccountGetName(gav->account), "Third") != 0)
{
PWARN("Wrong name %s for Third account",
xaccAccountGetName(gav->account));
pass = FALSE;
}
if (xaccAccountGetType(gav->account) != ACCOUNT_TYPE_LIABILITY)
{
PWARN("Third Account Type wrong, expected %d got %d",
ACCOUNT_TYPE_LIABILITY, xaccAccountGetType(gav->account));
pass = FALSE;
}
if (gnc_numeric_compare(value_3, gav->value) != 0)
{
PWARN("Third Account value wrong, expected %d/%d got %d/%d",
gnc_numeric_num(value_3), gnc_numeric_den(value_3),
gnc_numeric_num(gav->value), gnc_numeric_den(gav->value));
pass = FALSE;
}
xaccAccountDestroy(gav->account);
gav->account = NULL;
qof_book_destroy(book);
return pass;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment