mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Move gnc-euro.[ch] to engine and unit test it.
This commit is contained in:
parent
2774f8f88c
commit
d8417c3cfa
@ -23,7 +23,6 @@ set (app_utils_HEADERS
|
||||
gnc-accounting-period.h
|
||||
gnc-addr-quickfill.h
|
||||
gnc-entry-quickfill.h
|
||||
gnc-euro.h
|
||||
gnc-exp-parser.h
|
||||
gnc-gsettings.h
|
||||
gnc-help-utils.h
|
||||
@ -60,7 +59,6 @@ set (app_utils_SOURCES
|
||||
gnc-accounting-period.c
|
||||
gnc-addr-quickfill.c
|
||||
gnc-entry-quickfill.c
|
||||
gnc-euro.c
|
||||
gnc-exp-parser.c
|
||||
gnc-gsettings.cpp
|
||||
gnc-helpers.c
|
||||
|
@ -49,6 +49,7 @@ set (engine_HEADERS
|
||||
gnc-date.h
|
||||
gnc-datetime.hpp
|
||||
gnc-engine.h
|
||||
gnc-euro.h
|
||||
gnc-event.h
|
||||
gnc-features.h
|
||||
gnc-hooks.h
|
||||
@ -144,6 +145,7 @@ set (engine_SOURCES
|
||||
gnc-date.cpp
|
||||
gnc-datetime.cpp
|
||||
gnc-engine.c
|
||||
gnc-euro.c
|
||||
gnc-event.c
|
||||
gnc-features.c
|
||||
gnc-hooks.c
|
||||
|
@ -27,8 +27,7 @@
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "gnc-ui-util.h"
|
||||
#include "gnc-session.h"
|
||||
|
||||
/* local structs */
|
||||
typedef struct
|
||||
@ -39,7 +38,7 @@ typedef struct
|
||||
|
||||
|
||||
/* This array MUST be sorted ! */
|
||||
/* The rates are per EURO */
|
||||
/* The rates are per EURO and are converted to GncNumeric */
|
||||
static gnc_euro_rate_struct gnc_euro_rates[] =
|
||||
{
|
||||
{ "ATS", 13.7603 }, /* austrian schilling */
|
||||
@ -157,7 +156,8 @@ gnc_convert_to_euro(const gnc_commodity * currency, gnc_numeric value)
|
||||
rate = double_to_gnc_numeric (result->rate, 100000, GNC_HOW_RND_ROUND_HALF_UP);
|
||||
|
||||
/* EC Regulation 1103/97 states we should use "Round half away from zero"
|
||||
* See http://europa.eu/legislation_summaries/economic_and_monetary_affairs/institutional_and_economic_framework/l25025_en.htm */
|
||||
* See https://eur-lex.europa.eu/legal-content/EN/TXT/?uri=CELEX%3A31997R1103&qid=1662917247821
|
||||
*/
|
||||
return gnc_numeric_div (value, rate, 100, GNC_HOW_RND_ROUND_HALF_UP);
|
||||
}
|
||||
}
|
||||
@ -227,9 +227,8 @@ gnc_euro_currency_get_rate (const gnc_commodity *currency)
|
||||
gnc_commodity *
|
||||
gnc_get_euro (void)
|
||||
{
|
||||
gnc_commodity_table *table;
|
||||
|
||||
table = gnc_commodity_table_get_table (gnc_get_current_book ());
|
||||
QofBook* book = qof_session_get_book (gnc_get_current_session ());
|
||||
gnc_commodity_table *table = gnc_commodity_table_get_table (book);
|
||||
|
||||
return gnc_commodity_table_lookup (table, GNC_COMMODITY_NS_CURRENCY, "EUR");
|
||||
}
|
@ -132,6 +132,9 @@ set(test_qofsession_SOURCES
|
||||
gnc_add_test(test-qofsession "${test_qofsession_SOURCES}"
|
||||
gtest_engine_INCLUDES gtest_old_engine_LIBS)
|
||||
|
||||
gnc_add_test(test-gnc-euro gtest-gnc-euro.cpp
|
||||
gtest_engine_INCLUDES gtest_old_engine_LIBS)
|
||||
|
||||
set(test_gnc_int128_SOURCES
|
||||
${MODULEPATH}/gnc-int128.cpp
|
||||
gtest-gnc-int128.cpp)
|
||||
@ -197,6 +200,7 @@ gnc_add_test(test-qofquerycore "${test_qofquerycore_SOURCES}"
|
||||
|
||||
set(test_engine_SOURCES_DIST
|
||||
dummy.cpp
|
||||
gtest-gnc-euro.cpp
|
||||
gtest-gnc-int128.cpp
|
||||
gtest-gnc-rational.cpp
|
||||
gtest-gnc-numeric.cpp
|
||||
|
228
libgnucash/engine/test/gtest-gnc-euro.cpp
Normal file
228
libgnucash/engine/test/gtest-gnc-euro.cpp
Normal file
@ -0,0 +1,228 @@
|
||||
/********************************************************************
|
||||
* gtest-gnc-euro.cpp -- unit tests for Euro currency functions *
|
||||
* Copyright 2022 John Ralls <jralls@ceridwen.us> *
|
||||
* *
|
||||
* 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, 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 *
|
||||
\********************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "../gnc-numeric.hpp"
|
||||
extern "C"
|
||||
{
|
||||
#include <config.h>
|
||||
#include "../gnc-euro.h"
|
||||
#include "../gnc-commodity.h"
|
||||
#include "../gnc-session.h"
|
||||
}
|
||||
|
||||
class Currencies : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
gnc_commodity_table* m_table;
|
||||
Currencies() : m_table{gnc_commodity_table_new()}
|
||||
{
|
||||
QofBook* book = qof_session_get_book (gnc_get_current_session ());
|
||||
qof_book_set_data (book, GNC_COMMODITY_TABLE, m_table);
|
||||
|
||||
if (!gnc_commodity_table_add_default_data(m_table, book))
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
~Currencies() { gnc_commodity_table_destroy(m_table); }
|
||||
};
|
||||
|
||||
static const char* currency{"CURRENCY"};
|
||||
|
||||
TEST_F(Currencies, is_euro_currency)
|
||||
{
|
||||
auto usd = gnc_commodity_table_lookup(m_table, currency, "USD");
|
||||
auto eur = gnc_commodity_table_lookup(m_table, currency, "EUR");
|
||||
auto pte = gnc_commodity_table_lookup(m_table, currency, "PTE");
|
||||
|
||||
ASSERT_NE(eur, nullptr);
|
||||
ASSERT_NE(usd, nullptr);
|
||||
ASSERT_NE(pte, nullptr);
|
||||
|
||||
EXPECT_TRUE(gnc_is_euro_currency(pte));
|
||||
EXPECT_TRUE(gnc_is_euro_currency(eur));
|
||||
EXPECT_FALSE(gnc_is_euro_currency(usd));
|
||||
}
|
||||
|
||||
TEST_F(Currencies, convert_to_euro)
|
||||
{
|
||||
gnc_numeric value{314159, 100};
|
||||
gnc_numeric cyp_eur_amount{536776, 100}; // calc gets 5367.76
|
||||
auto cyp{gnc_commodity_table_lookup(m_table, currency, "CYP")};
|
||||
ASSERT_NE(cyp, nullptr);
|
||||
auto amount{gnc_convert_to_euro(cyp, value)};
|
||||
EXPECT_EQ(cyp_eur_amount.num, amount.num);
|
||||
EXPECT_EQ(cyp_eur_amount.denom, amount.denom);
|
||||
EXPECT_TRUE(gnc_numeric_equal(cyp_eur_amount, amount));
|
||||
|
||||
gnc_numeric grd_eur_amount{922, 100};
|
||||
auto grd{gnc_commodity_table_lookup(m_table, currency, "GRD")};
|
||||
amount = gnc_convert_to_euro(grd, value);
|
||||
EXPECT_EQ(grd_eur_amount.num, amount.num);
|
||||
EXPECT_EQ(grd_eur_amount.denom, amount.denom);
|
||||
EXPECT_TRUE(gnc_numeric_equal(grd_eur_amount, amount));
|
||||
|
||||
gnc_numeric itl_eur_amount{162, 100};
|
||||
auto itl{gnc_commodity_table_lookup(m_table, currency, "ITL")};
|
||||
amount = gnc_convert_to_euro(itl, value);
|
||||
EXPECT_EQ(itl_eur_amount.num, amount.num);
|
||||
EXPECT_EQ(itl_eur_amount.denom, amount.denom);
|
||||
EXPECT_TRUE(gnc_numeric_equal(itl_eur_amount, amount));
|
||||
|
||||
gnc_numeric nlg_eur_amount{142559, 100};
|
||||
auto nlg{gnc_commodity_table_lookup(m_table, currency, "NLG")};
|
||||
amount = gnc_convert_to_euro(nlg, value);
|
||||
EXPECT_EQ(nlg_eur_amount.num, amount.num);
|
||||
EXPECT_EQ(nlg_eur_amount.denom, amount.denom);
|
||||
EXPECT_TRUE(gnc_numeric_equal(nlg_eur_amount, amount));
|
||||
|
||||
auto eur{gnc_commodity_table_lookup(m_table, currency, "EUR")};
|
||||
ASSERT_NE(eur, nullptr);
|
||||
EXPECT_TRUE(gnc_numeric_equal(value,
|
||||
gnc_convert_to_euro(eur, value)));
|
||||
}
|
||||
|
||||
TEST_F(Currencies, convert_from_euro)
|
||||
{
|
||||
gnc_numeric value{314159, 100}; //.787564 * 3141.59 = 2472.20
|
||||
gnc_numeric eur_iep_amount{247419, 100};
|
||||
auto iep{gnc_commodity_table_lookup(m_table, currency, "IEP")};
|
||||
ASSERT_NE(iep, nullptr);
|
||||
auto amount{gnc_convert_from_euro(iep, value)};
|
||||
EXPECT_EQ(eur_iep_amount.num, amount.num);
|
||||
EXPECT_EQ(eur_iep_amount.denom, amount.denom);
|
||||
EXPECT_TRUE(gnc_numeric_equal(eur_iep_amount, amount));
|
||||
|
||||
gnc_numeric eur_itl_amount{6082966, 1};
|
||||
auto itl{gnc_commodity_table_lookup(m_table, currency, "ITL")};
|
||||
amount = gnc_convert_from_euro(itl, value);
|
||||
EXPECT_EQ(eur_itl_amount.num, amount.num);
|
||||
EXPECT_EQ(eur_itl_amount.denom, amount.denom);
|
||||
EXPECT_TRUE(gnc_numeric_equal(eur_itl_amount, amount));
|
||||
|
||||
gnc_numeric eur_fim_amount{1867905, 100};
|
||||
auto fim{gnc_commodity_table_lookup(m_table, currency, "FIM")};
|
||||
EXPECT_NE(fim, nullptr);
|
||||
amount = gnc_convert_from_euro(fim, value);
|
||||
EXPECT_EQ(eur_fim_amount.num, amount.num);
|
||||
EXPECT_EQ(eur_fim_amount.denom, amount.denom);
|
||||
EXPECT_TRUE(gnc_numeric_equal(eur_fim_amount, amount));
|
||||
|
||||
gnc_numeric eur_dm_amount{614442, 100};
|
||||
auto dm{gnc_commodity_table_lookup(m_table, currency, "DEM")};
|
||||
EXPECT_NE(dm, nullptr);
|
||||
amount = gnc_convert_from_euro(dm, value);
|
||||
EXPECT_EQ(eur_dm_amount.num, amount.num);
|
||||
EXPECT_EQ(eur_dm_amount.denom, amount.denom);
|
||||
EXPECT_TRUE(gnc_numeric_equal(eur_dm_amount, amount));
|
||||
|
||||
auto eur{gnc_commodity_table_lookup(m_table, currency, "EUR")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(value,
|
||||
gnc_convert_from_euro(eur, value)));
|
||||
}
|
||||
|
||||
TEST_F(Currencies, euro_currency_get_rate)
|
||||
{
|
||||
auto ats{gnc_commodity_table_lookup(m_table, currency, "ATS")};
|
||||
ASSERT_NE(ats, nullptr);
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{137603, 10000},
|
||||
gnc_euro_currency_get_rate(ats)));
|
||||
|
||||
auto bef{gnc_commodity_table_lookup(m_table, currency, "BEF")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{403399, 10000},
|
||||
gnc_euro_currency_get_rate(bef)));
|
||||
|
||||
auto cyp{gnc_commodity_table_lookup(m_table, currency, "CYP")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{585274, 1000000},
|
||||
gnc_euro_currency_get_rate(cyp)));
|
||||
|
||||
auto dem{gnc_commodity_table_lookup(m_table, currency, "DEM")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{195583, 100000},
|
||||
gnc_euro_currency_get_rate(dem)));
|
||||
|
||||
auto eek{gnc_commodity_table_lookup(m_table, currency, "EEK")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{156466, 10000},
|
||||
gnc_euro_currency_get_rate(eek)));
|
||||
|
||||
auto esp{gnc_commodity_table_lookup(m_table, currency, "ESP")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{166386, 1000},
|
||||
gnc_euro_currency_get_rate(esp)));
|
||||
|
||||
auto eur{gnc_commodity_table_lookup(m_table, currency, "EUR")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{100000, 100000},
|
||||
gnc_euro_currency_get_rate(eur)));
|
||||
|
||||
auto fim{gnc_commodity_table_lookup(m_table, currency, "FIM")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{594573, 100000},
|
||||
gnc_euro_currency_get_rate(fim)));
|
||||
|
||||
|
||||
auto frf{gnc_commodity_table_lookup(m_table, currency, "FRF")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{655957, 100000},
|
||||
gnc_euro_currency_get_rate(frf)));
|
||||
|
||||
auto grd{gnc_commodity_table_lookup(m_table, currency, "GRD")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{340750, 1000},
|
||||
gnc_euro_currency_get_rate(grd)));
|
||||
|
||||
|
||||
auto hrk{gnc_commodity_table_lookup(m_table, currency, "HRK")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{753450, 100000},
|
||||
gnc_euro_currency_get_rate(hrk)));
|
||||
|
||||
auto iep{gnc_commodity_table_lookup(m_table, currency, "IEP")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{787564, 1000000},
|
||||
gnc_euro_currency_get_rate(iep)));
|
||||
|
||||
auto itl{gnc_commodity_table_lookup(m_table, currency, "ITL")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{193627, 100},
|
||||
gnc_euro_currency_get_rate(itl)));
|
||||
|
||||
auto luf{gnc_commodity_table_lookup(m_table, currency, "LUF")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{403399, 10000},
|
||||
gnc_euro_currency_get_rate(luf)));
|
||||
|
||||
auto lvl{gnc_commodity_table_lookup(m_table, currency, "LVL")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{702804, 1000000},
|
||||
gnc_euro_currency_get_rate(lvl)));
|
||||
|
||||
auto mtl{gnc_commodity_table_lookup(m_table, currency, "MTL")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{429300, 1000000},
|
||||
gnc_euro_currency_get_rate(mtl)));
|
||||
|
||||
auto nlg{gnc_commodity_table_lookup(m_table, currency, "NLG")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{220371, 100000},
|
||||
gnc_euro_currency_get_rate(nlg)));
|
||||
|
||||
auto pte{gnc_commodity_table_lookup(m_table, currency, "PTE")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{200482, 1000},
|
||||
gnc_euro_currency_get_rate(pte)));
|
||||
|
||||
auto sit{gnc_commodity_table_lookup(m_table, currency, "SIT")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{239640, 1000},
|
||||
gnc_euro_currency_get_rate(sit)));
|
||||
|
||||
auto skk{gnc_commodity_table_lookup(m_table, currency, "SKK")};
|
||||
EXPECT_TRUE(gnc_numeric_equal(gnc_numeric{301260, 10000},
|
||||
gnc_euro_currency_get_rate(skk)));
|
||||
|
||||
}
|
@ -534,7 +534,6 @@ libgnucash/app-utils/gnc-accounting-period.c
|
||||
libgnucash/app-utils/gnc-account-merge.c
|
||||
libgnucash/app-utils/gnc-addr-quickfill.c
|
||||
libgnucash/app-utils/gnc-entry-quickfill.c
|
||||
libgnucash/app-utils/gnc-euro.c
|
||||
libgnucash/app-utils/gnc-exp-parser.c
|
||||
libgnucash/app-utils/gnc-gsettings.cpp
|
||||
libgnucash/app-utils/gnc-helpers.c
|
||||
@ -640,6 +639,7 @@ libgnucash/engine/gnc-datetime.cpp
|
||||
libgnucash/engine/gncEmployee.c
|
||||
libgnucash/engine/gnc-engine.c
|
||||
libgnucash/engine/gncEntry.c
|
||||
libgnucash/engine/gnc-euro.c
|
||||
libgnucash/engine/gnc-event.c
|
||||
libgnucash/engine/gnc-features.c
|
||||
libgnucash/engine/gnc-hooks.c
|
||||
|
Loading…
Reference in New Issue
Block a user