From 16af771277612ac56e03585b267555e4f31c7930 Mon Sep 17 00:00:00 2001
From: Niels Lohmann <niels.lohmann@gmail.com>
Date: Wed, 1 Feb 2017 17:34:39 +0100
Subject: [PATCH] :white_check_mark: added roundtrip test for the Big List of
 Naughty Strings

---
 README.md                    |  2 +-
 test/src/unit-testsuites.cpp | 45 +++++++++++++++++++++++++++++++++++-
 2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 6ebd30e4..5cb91abb 100644
--- a/README.md
+++ b/README.md
@@ -839,7 +839,7 @@ $ make json_unit -Ctest
 $ ./test/json_unit "*""
 
 ===============================================================================
-All tests passed (11202052 assertions in 47 test cases)
+All tests passed (11202549 assertions in 47 test cases)
 ```
 
 Alternatively, you can use [CMake](https://cmake.org) and run
diff --git a/test/src/unit-testsuites.cpp b/test/src/unit-testsuites.cpp
index 9a9de127..62e7e37e 100644
--- a/test/src/unit-testsuites.cpp
+++ b/test/src/unit-testsuites.cpp
@@ -816,13 +816,56 @@ TEST_CASE("nst's JSONTestSuite")
     }
 }
 
+// from http://stackoverflow.com/a/25829178/266378
+std::string trim(const std::string& str)
+{
+    size_t first = str.find_first_not_of(' ');
+    if (std::string::npos == first)
+    {
+        return str;
+    }
+    size_t last = str.find_last_not_of(' ');
+    return str.substr(first, (last - first + 1));
+}
+
 TEST_CASE("Big List of Naughty Strings")
 {
     // test from https://github.com/minimaxir/big-list-of-naughty-strings
-    SECTION("blns.json")
+    SECTION("parsing blns.json")
     {
         std::ifstream f("test/data/big-list-of-naughty-strings/blns.json");
         json j;
         CHECK_NOTHROW(j << f);
     }
+
+    // check if parsed strings roundtrip
+    // https://www.reddit.com/r/cpp/comments/5qpbie/json_form_modern_c_version_210/dd12mpq/
+    SECTION("roundtripping")
+    {
+        std::ifstream f("test/data/big-list-of-naughty-strings/blns.json");
+
+        while (not f.eof())
+        {
+            // read line
+            std::string line;
+            getline(f, line);
+
+            // trim whitespace
+            line = trim(line);
+
+            // remove trailing comma
+            line = line.substr(0, line.find_last_of(","));
+
+            // discard lines without at least two characters (quotes)
+            if (line.size() < 2)
+            {
+                continue;
+            }
+
+            // check roundtrip
+            CAPTURE(line);
+            json j = json::parse(line);
+            CHECK(j.dump() == line);
+        }
+    }
 }