From dacf84002aac362c43b69427d9e6c7fb42ad4404 Mon Sep 17 00:00:00 2001
From: Sam Yates <sam@quux.dropbear.id.au>
Date: Thu, 13 Oct 2016 12:51:16 +0200
Subject: [PATCH] Address PR comments

* Add unit test that demonstrates need for non-const version
  of `max_element_by`
* Rename file stream member variable from `f_` to `out_`
  in `validation_data.hpp`.
---
 tests/unit/test_range.cpp            |  8 ++++++++
 tests/validation/validation_data.hpp | 14 +++++++-------
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/tests/unit/test_range.cpp b/tests/unit/test_range.cpp
index 90df4067..f486d223 100644
--- a/tests/unit/test_range.cpp
+++ b/tests/unit/test_range.cpp
@@ -205,6 +205,14 @@ TEST(range, max_element_by) {
     EXPECT_TRUE((std::is_same<const char *, decltype(i)>::value));
     EXPECT_EQ('d', *i);
     EXPECT_EQ(cstr+9, i);
+
+    // with mutable container
+    std::vector<int> v = { 1, 3, -5, 2 };
+    auto j  = util::max_element_by(v, [](int x) { return x*x; });
+    EXPECT_EQ(-5, *j);
+    *j = 1;
+    j  = util::max_element_by(v, [](int x) { return x*x; });
+    EXPECT_EQ(3, *j);
 }
 
 TEST(range, max_value) {
diff --git a/tests/validation/validation_data.hpp b/tests/validation/validation_data.hpp
index d9026da3..70e99a52 100644
--- a/tests/validation/validation_data.hpp
+++ b/tests/validation/validation_data.hpp
@@ -34,9 +34,9 @@ public:
     }
 
     void write_traces() {
-        if (f_) {
-            f_ << jtraces_;
-            f_.close();
+        if (out_) {
+            out_ << jtraces_;
+            out_.close();
         }
     }
 
@@ -55,8 +55,8 @@ public:
     void set_datadir(const util::path& dir) { datadir_ = dir; }
 
     void set_output(const util::path& file) {
-        f_.open(file);
-        if (!f_) {
+        out_.open(file);
+        if (!out_) {
             throw std::runtime_error("unable to open file for writing");
         }
     }
@@ -64,14 +64,14 @@ public:
     // write traces on exit
 
     ~trace_io() {
-        if (f_) {
+        if (out_) {
             write_traces();
         }
     }
 
 private:
     util::path datadir_ = DATADIR "/validation";
-    std::ofstream f_;
+    std::ofstream out_;
     nlohmann::json jtraces_ = nlohmann::json::array();
     bool verbose_flag_ = false;
     int max_ncomp_ = 1000;
-- 
GitLab