Skip to content

Commit

Permalink
fix some compilation issues with the recommendation 20 unit inclusion…
Browse files Browse the repository at this point in the history
… and testing
  • Loading branch information
phlptp committed Oct 23, 2023
1 parent 6c60d21 commit 3a17e7e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/installation/cmake_variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CMake variables
- `UNITS_DOMAIN`: Specify a default domain to use for string conversions. Can be either a name from the domains namespace such as `domains::surveying` or one of 'COOKING', 'ASTRONOMY', 'NUCLEAR', 'SURVEYING', 'USE_CUSTOMARY', 'CLIMATE', or 'UCUM'.
- `UNITS_DEFAULT_MATCH_FLAGS`: Specify an integer value for the default match flags to be used for conversion
- `UNITS_DISABLE_NON_ENGLISH_UNITS`: the library includes a number of non-english units that can be converted from strings, these can be disabled by setting `UNITS_DISABLE_NON_ENGLISH_UNITS` to ON or setting the definition in the C++ code.
- `UNITS_DISABLE_EXTRA_UNIT_STANDARDS`: If set to `ON` disables UN recommendation 12, X12(not implemented yet), DOD(not implemented yet), from being included in the compilation and generated from strings.

- `UNITS_NAMESPACE`: The top level namespace of the library, defaults to `units`.
When compiling with C++17 (or higher), this can be set to, e.g., `mynamespace::units` to avoid name clashes with other libraries defining `units`.
Expand Down
11 changes: 10 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ set(UNITS_TESTS
test_commodities
test_siunits
test_defined_units
test_r20
test_math
test_google_units
)

if (NOT UNITS_DISABLE_EXTRA_UNIT_STANDARDS)
list(APPEND UNITS_TESTS test_r20)
endif()

set(TEST_FILE_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/files)

# /wd4459 is for a warning of a global m in google test. They won't interfere so ignore
Expand Down Expand Up @@ -69,6 +72,11 @@ else()
target_compile_definitions(
test_unit_strings PUBLIC -DENABLE_UNIT_TESTING=1 -DENABLE_UNIT_MAP_ACCESS=1
)
if (NOT UNITS_DISABLE_EXTRA_UNIT_STANDARDS)
target_compile_definitions (
test_r20 PUBLIC -DENABLE_UNIT_MAP_ACCESS=1
)
endif()

add_unit_test(test_leadingNumbers.cpp)
target_link_libraries(
Expand All @@ -83,6 +91,7 @@ else()
endif()
endif()


target_compile_definitions(
test_conversions2 PUBLIC -DTEST_FILE_FOLDER="${TEST_FILE_FOLDER}"
)
Expand Down
8 changes: 5 additions & 3 deletions test/test_r20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ SPDX-License-Identifier: BSD-3-Clause
#include <tuple>

using unitD = std::tuple<const char*, const char*, units::precise_unit>;

#ifdef ENABLE_UNIT_MAP_ACCESS
// Check the order of the array make sure it is order for searching
TEST(r20, order)
{
std::size_t unit_count{0};
const void* r20 = units::detail::testing::r20rawData(unit_count);
const void* r20 = units::detail::r20rawData(unit_count);
auto* r20data = reinterpret_cast<const unitD*>(r20);
for (size_t ii = 1; ii < unit_count; ++ii) {
EXPECT_LT(
Expand All @@ -29,7 +29,7 @@ TEST(r20, order)
TEST(r20, conversions)
{
std::size_t unit_count{0};
const void* r20 = units::detail::testing::r20rawData(unit_count);
const void* r20 = units::detail::r20rawData(unit_count);
auto* r20data = reinterpret_cast<const unitD*>(r20);
int missed{0};
int correct{0};
Expand Down Expand Up @@ -124,3 +124,5 @@ TEST(r20, conversions)
std::cout << convertible << " r20 unit are convertible to eachother\n";
std::cout << correct << " r20 units correctly translated\n";
}

#endif
17 changes: 16 additions & 1 deletion units/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
# See the top-level NOTICE for additional details. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set(units_source_files units.cpp x12_conv.cpp r20_conv.cpp commodities.cpp)


if (UNITS_DISABLE_EXTRA_UNIT_STANDARDS)
set(units_source_files units.cpp commodities.cpp)
else()
set(units_source_files units.cpp commodities.cpp x12_conv.cpp r20_conv.cpp)
endif()

set(units_header_files
units.hpp
Expand Down Expand Up @@ -60,6 +66,9 @@ if(UNITS_BUILD_SHARED_LIBRARY)
if(UNITS_DISABLE_NON_ENGLISH_UNITS)
target_compile_definitions(units PUBLIC -DUNITS_DISABLE_NON_ENGLISH_UNITS=1)
endif()
if (UNITS_DISABLE_EXTRA_UNIT_STANDARDS)
target_compile_definitions(units PUBLIC -DUNITS_DISABLE_EXTRA_UNIT_STANDARDS=1)
endif()
if(UNITS_DEFAULT_DOMAIN)
target_compile_definitions(
units PRIVATE -DUNITS_DEFAULT_DOMAIN=${UNITS_DEFAULT_DOMAIN}
Expand Down Expand Up @@ -106,6 +115,9 @@ elseif(UNITS_BUILD_OBJECT_LIBRARY)
if(UNITS_DISABLE_NON_ENGLISH_UNITS)
target_compile_definitions(units PUBLIC -DUNITS_DISABLE_NON_ENGLISH_UNITS=1)
endif()
if (UNITS_DISABLE_EXTRA_UNIT_STANDARDS)
target_compile_definitions(units PUBLIC -DUNITS_DISABLE_EXTRA_UNIT_STANDARDS=1)
endif()
if(${CMAKE_CXX_STANDARD} GREATER 16)
target_compile_definitions(units PRIVATE -DUNITS_CONSTEXPR_IF_SUPPORTED=1)
endif()
Expand All @@ -130,6 +142,9 @@ elseif(UNITS_BUILD_STATIC_LIBRARY)
if(UNITS_BASE_TYPE)
target_compile_definitions(units PUBLIC -DUNITS_BASE_TYPE=${UNITS_BASE_TYPE})
endif()
if (UNITS_DISABLE_EXTRA_UNIT_STANDARDS)
target_compile_definitions(units PUBLIC -DUNITS_DISABLE_EXTRA_UNIT_STANDARDS=1)
endif()
if(UNITS_DEFAULT_DOMAIN)
target_compile_definitions(
units PRIVATE -DUNITS_DEFAULT_DOMAIN=${UNITS_DEFAULT_DOMAIN}
Expand Down
7 changes: 5 additions & 2 deletions units/r20_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3138,13 +3138,16 @@ precise_unit r20_unit(const std::string& r20_string)
}
return precise::error;
}

#ifdef ENABLE_UNIT_MAP_ACCESS
namespace detail {
namespace testing {
const void* r20rawData(size_t& array_size)
{
array_size = precise::r20_units.size();
return precise::r20_units.data();
}
} // namespace testing
} // namespace detail

#endif

} // namespace units
9 changes: 5 additions & 4 deletions units/units.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2269,10 +2269,6 @@ namespace detail {
const char* unit,
int power,
std::uint64_t flags);
#ifdef EXTRA_UNIT_STANDARDS
// get the raw array for testing the r20 database
const void* r20rawData(size_t& array_size);
#endif
} // namespace testing
} // namespace detail
#endif
Expand All @@ -2282,6 +2278,11 @@ namespace detail {
UNITS_EXPORT const std::unordered_map<std::string, precise_unit>&
getUnitStringMap();
UNITS_EXPORT const std::unordered_map<unit, const char*>& getUnitNameMap();

#ifdef EXTRA_UNIT_STANDARDS
// get the raw array for testing the r20 database
const void* r20rawData(size_t& array_size);
#endif
} // namespace detail
#endif

Expand Down

0 comments on commit 3a17e7e

Please sign in to comment.