test demand data passing
This commit is contained in:
15
src/demand.c
15
src/demand.c
@@ -11,7 +11,14 @@
|
|||||||
******************************************************************************
|
******************************************************************************
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#ifdef _DEBUG
|
||||||
|
#define _CRTDBG_MAP_ALLOC
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <crtdbg.h>
|
||||||
|
#else
|
||||||
|
#include <stdlib.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "demand.h"
|
#include "demand.h"
|
||||||
@@ -35,13 +42,14 @@ demand_data_t *create_demand_data(double base_demand, int pattern_index, char *c
|
|||||||
if (category_name)
|
if (category_name)
|
||||||
demand_data->category_name = strdup(category_name);
|
demand_data->category_name = strdup(category_name);
|
||||||
else
|
else
|
||||||
demand_data->category_name[0] = '\0';
|
demand_data->category_name = NULL;
|
||||||
|
|
||||||
return demand_data;
|
return demand_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete_demand_data(void *data)
|
void delete_demand_data(void *data)
|
||||||
{
|
{
|
||||||
|
// TODO: This cast is a problem!
|
||||||
demand_data_t *demand_data = *(demand_data_t **)data;
|
demand_data_t *demand_data = *(demand_data_t **)data;
|
||||||
|
|
||||||
if (demand_data->category_name)
|
if (demand_data->category_name)
|
||||||
@@ -92,10 +100,11 @@ void set_pattern_index(list_node_t *lnode, int pattern_index)
|
|||||||
|
|
||||||
char *get_category_name(list_node_t *lnode)
|
char *get_category_name(list_node_t *lnode)
|
||||||
{
|
{
|
||||||
return get_demand_data(lnode)->category_name;
|
return strdup(get_demand_data(lnode)->category_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_category_name(list_node_t *lnode, char *category_name)
|
void set_category_name(list_node_t *lnode, char *category_name)
|
||||||
{
|
{
|
||||||
|
free(get_demand_data(lnode)->category_name);
|
||||||
get_demand_data(lnode)->category_name = strdup(category_name);
|
get_demand_data(lnode)->category_name = strdup(category_name);
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/demand.h
10
src/demand.h
@@ -18,6 +18,11 @@
|
|||||||
#include "util/list.h"
|
#include "util/list.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// Forward declarations
|
// Forward declarations
|
||||||
typedef struct demand_data_s demand_data_t;
|
typedef struct demand_data_s demand_data_t;
|
||||||
|
|
||||||
@@ -47,4 +52,9 @@ void set_category_name(list_node_t *lnode, char *category_name);
|
|||||||
demand_data_t *get_demand_data(list_node_t *lnode);
|
demand_data_t *get_demand_data(list_node_t *lnode);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#endif /* DEMAND_H */
|
#endif /* DEMAND_H */
|
||||||
|
|||||||
@@ -23,6 +23,15 @@ add_test(NAME test_net_builder
|
|||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/data)
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/data)
|
||||||
|
|
||||||
|
|
||||||
|
add_executable(test_demand_data test_demand_data.cpp
|
||||||
|
../src/demand.c
|
||||||
|
../src/util/list.c)
|
||||||
|
target_include_directories(test_demand_data PUBLIC ../src/ ../src/util/)
|
||||||
|
target_link_libraries(test_demand_data ${Boost_LIBRARIES})
|
||||||
|
add_test(NAME test_demand_data
|
||||||
|
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_demand_data
|
||||||
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/data)
|
||||||
|
|
||||||
|
|
||||||
set(toolkit_test_srcs
|
set(toolkit_test_srcs
|
||||||
test_toolkit.cpp
|
test_toolkit.cpp
|
||||||
|
|||||||
@@ -17,9 +17,122 @@
|
|||||||
#include "demand.h"
|
#include "demand.h"
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(test_filemanager)
|
boost::test_tools::predicate_result check_string(std::string test, std::string ref)
|
||||||
|
{
|
||||||
|
if (ref.compare(test) == 0)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(test_demand_data)
|
||||||
|
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE (test_create_destroy)
|
||||||
|
{
|
||||||
|
void *data = NULL;
|
||||||
|
|
||||||
|
data = create_demand_data(100.0, 1, NULL);
|
||||||
|
BOOST_CHECK(data != NULL);
|
||||||
|
|
||||||
|
delete_demand_data(&data);
|
||||||
|
|
||||||
|
data = NULL;
|
||||||
|
|
||||||
|
data = create_demand_data(100.0, 1, "CUB_SCOUT_BASE_CAMP");
|
||||||
|
BOOST_CHECK(data != NULL);
|
||||||
|
|
||||||
|
delete_demand_data(&data);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_get_size)
|
||||||
|
{
|
||||||
|
size_t size = get_demand_data_size();
|
||||||
|
BOOST_CHECK(size == 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct Fixture {
|
||||||
|
Fixture() {
|
||||||
|
_data = NULL;
|
||||||
|
dlist = NULL;
|
||||||
|
|
||||||
|
dlist = create_list(get_demand_data_size(), delete_demand_data);
|
||||||
|
_data = create_demand_data(100.0, 1, "CUB_SCOUT_BASE_CAMP");
|
||||||
|
|
||||||
|
append_list(dlist, &_data);
|
||||||
|
|
||||||
|
}
|
||||||
|
~Fixture() {
|
||||||
|
delete_list(dlist);
|
||||||
|
}
|
||||||
|
void *_data;
|
||||||
|
list_t *dlist;
|
||||||
|
};
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(test_demand_list, Fixture)
|
||||||
|
{
|
||||||
|
list_node_t *lnode = head_list(dlist, false);
|
||||||
|
BOOST_CHECK(lnode != NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(test_demand_getset, Fixture)
|
||||||
|
{
|
||||||
|
list_node_t *lnode = head_list(dlist, false);
|
||||||
|
double demand;
|
||||||
|
|
||||||
|
demand = get_base_demand(lnode);
|
||||||
|
BOOST_CHECK(demand == 100.0);
|
||||||
|
|
||||||
|
set_base_demand(lnode, 200.0);
|
||||||
|
|
||||||
|
demand = get_base_demand(lnode);
|
||||||
|
BOOST_CHECK(demand == 200.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(test_pattern_getset, Fixture)
|
||||||
|
{
|
||||||
|
list_node_t *lnode = head_list(dlist, false);
|
||||||
|
int index;
|
||||||
|
|
||||||
|
index = get_pattern_index(lnode);
|
||||||
|
BOOST_CHECK(index == 1);
|
||||||
|
|
||||||
|
set_pattern_index(lnode, 2);
|
||||||
|
|
||||||
|
index = get_pattern_index(lnode);
|
||||||
|
BOOST_CHECK(index == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(test_category_getset, Fixture)
|
||||||
|
{
|
||||||
|
list_node_t *lnode = head_list(dlist, false);
|
||||||
|
char *name;
|
||||||
|
|
||||||
|
name = get_category_name(lnode);
|
||||||
|
BOOST_CHECK(check_string(name, "CUB_SCOUT_BASE_CAMP"));
|
||||||
|
|
||||||
|
free(name);
|
||||||
|
|
||||||
|
set_category_name(lnode, "CUB_SCOUT_COMMAND");
|
||||||
|
|
||||||
|
name = get_category_name(lnode);
|
||||||
|
BOOST_CHECK(check_string(name, "CUB_SCOUT_COMMAND"));
|
||||||
|
|
||||||
|
free(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(test_convert_demand, Fixture)
|
||||||
|
{
|
||||||
|
list_node_t *lnode = head_list(dlist, false);
|
||||||
|
BOOST_CHECK(lnode != NULL);
|
||||||
|
|
||||||
|
// 100.0 GPM == 6.31 LPS
|
||||||
|
convert_units(lnode, 15.850);
|
||||||
|
double demand = get_base_demand(lnode);
|
||||||
|
BOOST_TEST(demand == 6.31, boost::test_tools::tolerance(0.01));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|||||||
Reference in New Issue
Block a user