Permalink
Browse files

Fixed a few more places where doubles were not serialised to full acc…

…uracy
  • Loading branch information...
tpoole committed Feb 11, 2019
1 parent bd07014 commit 306e7e43608cbea50de5d04961b1057b8be8a09e
@@ -175,7 +175,7 @@ class var::VariantType_Double : public var::VariantType
int toInt (const ValueUnion& data) const noexcept override { return (int) data.doubleValue; }
int64 toInt64 (const ValueUnion& data) const noexcept override { return (int64) data.doubleValue; }
double toDouble (const ValueUnion& data) const noexcept override { return data.doubleValue; }
String toString (const ValueUnion& data) const override { return String (data.doubleValue, 20); }
String toString (const ValueUnion& data) const override { return minimiseLengthOfFloatString (String (data.doubleValue, 15, true)); }
bool toBool (const ValueUnion& data) const noexcept override { return data.doubleValue != 0.0; }
bool isDouble() const noexcept override { return true; }
bool isComparable() const noexcept override { return true; }
@@ -110,13 +110,14 @@ class JUCE_API JSON
/** Writes a JSON-formatted representation of the var object to the given stream.
If allOnOneLine is true, the result will be compacted into a single line of text
with no carriage-returns. If false, it will be laid-out in a more human-readable format.
The maximumDecimalPlaces parameter determines the precision of floating point numbers.
The maximumDecimalPlaces parameter determines the precision of floating point numbers
in scientific notation.
@see toString
*/
static void writeToStream (OutputStream& output,
const var& objectToFormat,
bool allOnOneLine = false,
int maximumDecimalPlaces = 20);
int maximumDecimalPlaces = 15);

/** Returns a version of a string with any extended characters escaped. */
static String escapeString (StringRef);
@@ -125,7 +125,6 @@
#include "containers/juce_PropertySet.cpp"
#include "containers/juce_ReferenceCountedArray.cpp"
#include "containers/juce_SparseSet.cpp"
#include "containers/juce_Variant.cpp"
#include "files/juce_DirectoryIterator.cpp"
#include "files/juce_File.cpp"
#include "files/juce_FileInputStream.cpp"
@@ -172,6 +171,7 @@
#include "time/juce_RelativeTime.cpp"
#include "time/juce_Time.cpp"
#include "unit_tests/juce_UnitTest.cpp"
#include "containers/juce_Variant.cpp"
#include "javascript/juce_JSON.cpp"
#include "javascript/juce_Javascript.cpp"
#include "containers/juce_DynamicObject.cpp"
@@ -1157,32 +1157,63 @@ class ValueTreeTests : public UnitTest

void runTest() override
{
beginTest ("ValueTree");
auto r = getRandom();

for (int i = 10; --i >= 0;)
{
MemoryOutputStream mo;
auto v1 = createRandomTree (nullptr, 0, r);
v1.writeToStream (mo);
beginTest ("ValueTree");

MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
auto v2 = ValueTree::readFromStream (mi);
expect (v1.isEquivalentTo (v2));
auto r = getRandom();

MemoryOutputStream zipped;
for (int i = 10; --i >= 0;)
{
GZIPCompressorOutputStream zippedOut (zipped);
v1.writeToStream (zippedOut);
}
expect (v1.isEquivalentTo (ValueTree::readFromGZIPData (zipped.getData(), zipped.getDataSize())));
MemoryOutputStream mo;
auto v1 = createRandomTree (nullptr, 0, r);
v1.writeToStream (mo);

MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
auto v2 = ValueTree::readFromStream (mi);
expect (v1.isEquivalentTo (v2));

MemoryOutputStream zipped;
{
GZIPCompressorOutputStream zippedOut (zipped);
v1.writeToStream (zippedOut);
}
expect (v1.isEquivalentTo (ValueTree::readFromGZIPData (zipped.getData(), zipped.getDataSize())));

std::unique_ptr<XmlElement> xml1 (v1.createXml());
std::unique_ptr<XmlElement> xml2 (v2.createCopy().createXml());
expect (xml1->isEquivalentTo (xml2.get(), false));
std::unique_ptr<XmlElement> xml1 (v1.createXml());
std::unique_ptr<XmlElement> xml2 (v2.createCopy().createXml());
expect (xml1->isEquivalentTo (xml2.get(), false));

auto v4 = v2.createCopy();
expect (v1.isEquivalentTo (v4));
auto v4 = v2.createCopy();
expect (v1.isEquivalentTo (v4));
}
}

{
beginTest ("Float formatting");

ValueTree testVT ("Test");
Identifier number ("number");

std::map<double, String> tests;
tests[1] = "1";
tests[1.1] = "1.1";
tests[1.01] = "1.01";
tests[0.76378] = "7.6378e-1";
tests[-10] = "-1e1";
tests[10.01] = "1.001e1";
tests[0.0123] = "1.23e-2";
tests[-3.7e-27] = "-3.7e-27";
tests[1e+40] = "1e40";

for (auto& test : tests)
{
testVT.setProperty (number, test.first, nullptr);
auto lines = StringArray::fromLines (testVT.toXmlString());
lines.removeEmptyStrings();
auto numLines = lines.size();
expect (numLines > 1);
expectEquals (lines[numLines - 1], "<Test number=\"" + test.second + "\"/>");
}
}
}
};

0 comments on commit 306e7e4

Please sign in to comment.