#include "third_party/nonstd/optional.hpp"
#include "third_party/nonstd/string_view.hpp"
+#include <algorithm>
#include <functional>
#include <ios>
#include <memory>
std::string change_extension(nonstd::string_view path,
nonstd::string_view new_ext);
+// Return `value` adjusted to not be less than `min` and not more than `max`.
+template<typename T>
+T
+clamp(T value, T min, T max)
+{
+ return std::min(max, std::max(min, value));
+}
+
// Clone a file from `src` to `dest`. If `via_tmp_file` is true, `src` is cloned
// to a temporary file and then renamed to `dest`. Throws `Error` on error.
void clone_file(const std::string& src,
}
ctx.config.set_limit_multiple(
- std::min(std::max(ctx.config.limit_multiple(), 0.0), 1.0));
+ Util::clamp(ctx.config.limit_multiple(), 0.0, 1.0));
MTR_BEGIN("main", "guess_compiler");
ctx.guessed_compiler = guess_compiler(ctx.orig_args[0]);
#include <algorithm>
+using doctest::Approx;
using nonstd::nullopt;
using TestUtil::TestContext;
CHECK(Util::change_extension("foo.bar.txt", ".o") == "foo.bar.o");
}
+TEST_CASE("Util::clamp")
+{
+ CHECK(Util::clamp(0, 1, 2) == 1);
+ CHECK(Util::clamp(1, 1, 2) == 1);
+ CHECK(Util::clamp(2, 1, 2) == 2);
+ CHECK(Util::clamp(3, 1, 2) == 2);
+
+ CHECK(Util::clamp(7.0, 7.7, 8.8) == Approx(7.7));
+ CHECK(Util::clamp(8.0, 7.7, 8.8) == Approx(8.0));
+ CHECK(Util::clamp(9.0, 7.7, 8.8) == Approx(8.8));
+}
+
TEST_CASE("Util::common_dir_prefix_length")
{
CHECK(Util::common_dir_prefix_length("", "") == 0);