Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header only target #23

Merged
merged 6 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/make_and_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set -evx

mkdir -p build
cd build
cmake .. -DCMAKE_CXX_STANDARD=$STD -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER_LAUNCHER=ccache $@
cmake .. -DCMAKE_CXX_STANDARD=$STD -DCMAKE_BUILD_TYPE=Debug $2 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache $@
cmake --build . -- -j2

set +evx
Expand Down
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ matrix:
- CCACHE_CPP2=yes
script:
- .ci/make_and_test.sh 11
- .ci/make_and_test.sh 14 -DUNITS_HEADER_ONLY=ON
# - .ci/make_and_test.sh 14
# - .ci/make_and_test.sh 17

Expand Down Expand Up @@ -74,9 +75,9 @@ matrix:
- ". .ci/build_lcov.sh"
- "bash .ci/run_codecov.sh"
script:
- .ci/make_and_test.sh 11
- .ci/make_and_test.sh 14
# - .ci/make_and_test.sh 17
- .ci/make_and_test.sh 11
- .ci/make_and_test.sh 14
- .ci/make_and_test.sh 17


# GCC 4.8
Expand Down
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(compiler_flags)
endif()

if(UNITS_HEADER_ONLY)
target_compile_definitions(units PUBLIC UNITS_HEADER_ONLY)
else(UNITS_HEADER_ONLY)
if(NOT UNITS_HEADER_ONLY)
if(BUILD_SHARED_LIBS)
option(UNITS_BUILD_STATIC_LIBRARY
"enable Construction of the units static library" OFF
Expand All @@ -94,7 +92,7 @@ else(UNITS_HEADER_ONLY)
OFF "NOT CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME" OFF
)

endif(UNITS_HEADER_ONLY)
endif(NOT UNITS_HEADER_ONLY)

add_subdirectory(units)

Expand Down
2 changes: 1 addition & 1 deletion FuzzTargets/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM helics/buildenv:clang-builder8 as builder
RUN git clone https://github.com/LLNL/units.git units

WORKDIR /root/develop/build
RUN cmake ../units -DUNITS_BUILD_FUZZ_TARGETS=ON -DUNITS_CXX_STANDARD=17 -DUNITS_INSTALL=OFF -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_COMPILER_FORCED=ON -DCMAKE_CXX_FLAGS="-g -O1 -fsanitize=fuzzer" && make -j2
RUN cmake ../units -DUNITS_BUILD_FUZZ_TARGETS=ON -DCMAKE_CXX_STANDARD=17 -DUNITS_INSTALL=OFF -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_COMPILER_FORCED=ON -DCMAKE_CXX_FLAGS="-g -O1 -fsanitize=fuzzer" && make -j2

WORKDIR /root/develop/fuzz_targets

Expand Down
2 changes: 1 addition & 1 deletion FuzzTargets/fuzz_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mkdir -p /fuzz/corpus
mkdir -p /fuzz/logs

# run the first fuzzing sequence
./fuzz_from_string /fuzz/corpus -max_len=512 -dict=/root/develop/fuzz_targets/fuzz_dictionary.txt -rss_limit_mb=512 -max_total_time=1435 -timeout=20 -artifact_prefix=/fuzz/ &> fuzzlog.txt
./fuzz_from_string /fuzz/corpus -max_len=512 -dict=/root/develop/fuzz_targets/fuzz_dictionary.txt -rss_limit_mb=512 -max_total_time=1430 -timeout=20 -artifact_prefix=/fuzz/ &> fuzzlog.txt

#if we haven't failed merge the corpus so it is smaller
if [ $? -eq 0 ]
Expand Down
31 changes: 25 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,40 @@

include(AddGoogletest)

set(UNITS_TESTS
fuzz_issue_tests
set(UNIT_TEST_HEADER_ONLY
test_conversions1
test_conversions2
test_equation_units
test_measurement
test_pu
test_unit_ops
test_uncertain_measurements
)

set(UNITS_TESTS
${UNIT_TEST_HEADER_ONLY}
fuzz_issue_tests
test_conversions2
test_random_round_trip
test_ucum
test_udunits
test_unit_ops
test_unit_strings
test_measurement_strings
test_commodities
test_commodities
test_leadingNumbers
test_uncertain_measurements
)

set(TEST_FILE_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/files)

if (UNITS_HEADER_ONLY)

foreach(T ${UNIT_TEST_HEADER_ONLY})

add_unit_test(${T}.cpp)
target_link_libraries(${T} units::units-header-only compile_flags_target)

endforeach()

else()
foreach(T ${UNITS_TESTS})

add_unit_test(${T}.cpp)
Expand Down Expand Up @@ -61,6 +75,9 @@ if(NOT MSVC)
target_compile_options(test_ucum PRIVATE -Wno-double-promotion)
target_compile_options(test_udunits PRIVATE -Wno-double-promotion)
endif()



add_executable(examples_test examples_test.cpp)
target_link_libraries(examples_test PRIVATE units::units)
target_compile_definitions(examples_test PRIVATE ${TARGET_SPECIAL_COMPILER_FLAGS})
Expand All @@ -86,3 +103,5 @@ if(CMAKE_BUILD_TYPE STREQUAL Coverage)
examples_test
)
endif()

endif() # UNITS_HEADER_ONLY
Binary file added test/files/fuzz_issues/rtrip_flag6
Binary file not shown.
22 changes: 18 additions & 4 deletions test/fuzz_issue_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ TEST_P(rtripProblems, rtripFiles)
} else if (!is_error(root(u2, 3))) {
EXPECT_EQ(root(unit_cast(u2), 3), root(unit_cast(u1), 3));
EXPECT_FALSE(root(units::unit_cast(u2), 3) != root(units::unit_cast(u1), 3));
} else {
EXPECT_EQ(unit_cast(u2), unit_cast(u1));
EXPECT_FALSE(units::unit_cast(u2) != units::unit_cast(u1));
}
}
}
Expand All @@ -246,10 +249,21 @@ TEST_P(rtripflagProblems, rtripflagFiles)
auto str = to_string(u1);
auto u2 = unit_from_string(str);
EXPECT_FALSE(is_error(u2));
EXPECT_EQ(u2, u1);
EXPECT_EQ(unit_cast(u2), unit_cast(u1));
EXPECT_FALSE(units::unit_cast(u2) != units::unit_cast(u1));
if (u2 == u1) {
EXPECT_EQ(u2, u1);
EXPECT_EQ(unit_cast(u2), unit_cast(u1));
EXPECT_FALSE(units::unit_cast(u2) != units::unit_cast(u1));
} else if (!is_error(root(u2, 2))) {
EXPECT_EQ(root(unit_cast(u2), 2), root(unit_cast(u1), 2));
EXPECT_FALSE(root(units::unit_cast(u2), 2) != root(units::unit_cast(u1), 2));
} else if (!is_error(root(u2, 3))) {
EXPECT_EQ(root(unit_cast(u2), 3), root(unit_cast(u1), 3));
EXPECT_FALSE(root(units::unit_cast(u2), 3) != root(units::unit_cast(u1), 3));
} else {
EXPECT_TRUE(unit_cast(u2) == unit_cast(u1));
EXPECT_FALSE(units::unit_cast(u2) != units::unit_cast(u1));
}
}
}

INSTANTIATE_TEST_SUITE_P(rtripflagFiles, rtripflagProblems, ::testing::Range(1, 6));
INSTANTIATE_TEST_SUITE_P(rtripflagFiles, rtripflagProblems, ::testing::Range(1, 6));
12 changes: 8 additions & 4 deletions test/test_measurement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ TEST(measurement, powroot)
auto v1 = pow(m1, 3);
EXPECT_EQ(v1.value(), 8.0);
EXPECT_EQ(v1.units(), m.pow(3));

#ifndef UNITS_HEADER_ONLY
auto m2 = root(v1, 3);
EXPECT_TRUE(m2 == m1);

Expand All @@ -150,6 +150,7 @@ TEST(measurement, powroot)

measurement m4(16.0, m.pow(2));
EXPECT_EQ(sqrt(m4), measurement(4.0, m));
#endif
}

using namespace units;
Expand Down Expand Up @@ -346,12 +347,13 @@ TEST(fixedMeasurement, powroot)
auto v1 = pow(m1, 3);
EXPECT_EQ(v1.value(), 8.0);
EXPECT_EQ(v1.units(), m.pow(3));

#ifndef UNITS_HEADER_ONLY
auto m2 = root(v1, 3);
EXPECT_TRUE(m2 == m1);

fixed_measurement m4(16.0, m.pow(2));
EXPECT_TRUE(sqrt(m4) == fixed_measurement(4.0, m));
#endif
}

TEST(PreciseMeasurement, ops)
Expand Down Expand Up @@ -473,11 +475,12 @@ TEST(PreciseMeasurement, powroot)
auto v1 = pow(m1, 3);
EXPECT_EQ(v1.value(), 8.0);
EXPECT_EQ(v1.units(), precise::m.pow(3));

#ifndef UNITS_HEADER_ONLY
auto m2 = root(v1, 3);
EXPECT_TRUE(m2 == m1);
precise_measurement m4(16.0, precise::m.pow(2));
EXPECT_EQ(sqrt(m4), precise_measurement(4.0, precise::m));
#endif
}

using namespace units;
Expand Down Expand Up @@ -520,10 +523,11 @@ TEST(fixedPreciseMeasurement, powroot)
auto v1 = pow(m1, 3);
EXPECT_EQ(v1.value(), 8.0);
EXPECT_EQ(v1.units(), precise::m.pow(3));

#ifndef UNITS_HEADER_ONLY
auto m2 = root(v1, 3);
EXPECT_TRUE(m2 == m1);

fixed_precise_measurement m4(16.0, precise::m.pow(2));
EXPECT_TRUE(sqrt(m4) == fixed_precise_measurement(4.0, precise::m));
#endif
}
4 changes: 4 additions & 0 deletions test/test_uncertain_measurements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ TEST(uncertainOps, conversion)
EXPECT_EQ(wc.units(), m);
}

#ifndef UNITS_HEADER_ONLY
// http://www.geol.lsu.edu/jlorenzo/geophysics/uncertainties/Uncertaintiespart2.html example c
// as a note the example on this page is just wrong, the calculations don't match the numbers given
TEST(uncertainOps, pow1)
Expand All @@ -266,6 +267,7 @@ TEST(uncertainOps, pow1)
EXPECT_NEAR(zs.value(), 29, 0.5);
EXPECT_NEAR(zs.uncertainty(), 12, 0.5);
}
#endif

// examples from http://lectureonline.cl.msu.edu/~mmp/labs/error/e2.htm

Expand Down Expand Up @@ -395,6 +397,7 @@ TEST(uncertainOps, testHeight)
EXPECT_EQ(ys.units(), m);
}

#ifndef UNITS_HEADER_ONLY
TEST(uncertainStrings, test1)
{
auto um1 = uncertain_measurement_from_string("12+/-3 m");
Expand Down Expand Up @@ -423,6 +426,7 @@ TEST(uncertainStrings, test1)
EXPECT_EQ(um5.uncertainty(), 0.0);
EXPECT_EQ(um4.units(), m);
}
#endif

TEST(uncertainOps, fractional_uncertainty)
{
Expand Down
4 changes: 4 additions & 0 deletions test/test_unit_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ TEST(unitOps, power)
EXPECT_EQ(m.inv().inv(), m.pow(-1).pow(-1));
}

#ifndef UNITS_HEADER_ONLY
TEST(unitOps, root)
{
EXPECT_EQ(root(m, 0), one);
Expand Down Expand Up @@ -97,6 +98,7 @@ TEST(unitOps, root)

EXPECT_EQ(root(unit(-4.5, m), 2), error);
}
#endif

TEST(unitOps, nan)
{
Expand Down Expand Up @@ -321,6 +323,7 @@ TEST(preciseUnitOps, Power)
EXPECT_EQ(m4_b, m2 * m2);
}

#ifndef UNITS_HEADER_ONLY
TEST(preciseUnitOps, root)
{
auto m1 = precise::m.pow(1);
Expand Down Expand Up @@ -355,6 +358,7 @@ TEST(preciseUnitOps, root)

EXPECT_TRUE(is_error(root(precise_unit(-4.5, m), 2)));
}
#endif

TEST(preciseUnitOps, nan)
{
Expand Down
13 changes: 12 additions & 1 deletion units/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ set(units_source_files units.cpp x12_conv.cpp r20_conv.cpp commodities.cpp)
set(units_header_files units.hpp units_decl.hpp unit_definitions.hpp)

if(UNITS_HEADER_ONLY)
# TODO: install units_Sources add this directory to the include path
add_library(units-header-only INTERFACE)
target_include_directories(
units-header-only INTERFACE $<BUILD_INTERFACE:${UNITS_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_definitions(units-header-only INTERFACE UNITS_HEADER_ONLY)
if(UNITS_INSTALL)
install(
TARGETS units-header-only ${UNITS_LIBRARY_EXPORT_COMMAND}
)
endif()
add_library(units::units-header-only ALIAS units-header-only)
else(UNITS_HEADER_ONLY)
if(UNITS_BUILD_STATIC_LIBRARY)
add_library(units-static STATIC ${units_source_files} ${units_header_files})
Expand Down
13 changes: 9 additions & 4 deletions units/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,11 +923,16 @@ static std::string to_string_internal(precise_unit un, uint32_t match_flags)
if (isNumericalCharacter(prefix.front())) {
size_t cut;
double mx = getDoubleFromString(prefix, &cut);
auto str =
getMultiplierString(1.0 / mx, true) + tu.second + "/" + prefix.substr(cut);
if (beststr.empty() || str.size() < beststr.size()) {
beststr = str;
if (mx != 0.0) {
auto str =
getMultiplierString(1.0 / mx, true) + tu.second + "/" + prefix.substr(cut);
if (beststr.empty() || str.size() < beststr.size()) {
beststr = str;
}
} else {
return std::string(tu.second) + "/" + prefix;
}

} else {
return std::string(tu.second) + "/" + prefix;
}
Expand Down
2 changes: 1 addition & 1 deletion webserver/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM helics/buildenv:tumbleweed-builder as builder
RUN git clone https://github.com/LLNL/units.git units

WORKDIR /root/develop/build
RUN cmake ../units -DUNITS_BUILD_WEBSERVER=ON -DBUILD_TESTING=OFF -DUNITS_CXX_STANDARD=17 -DCMAKE_BUILD_TYPE=Release && make -j2
RUN cmake ../units -DUNITS_BUILD_WEBSERVER=ON -DBUILD_TESTING=OFF -DCMAKE_CXX_STANDARD=17 -DCMAKE_BUILD_TYPE=Release && make -j2

WORKDIR /root/develop/webserver

Expand Down