Skip to content

Instantly share code, notes, and snippets.

@jmjatlanta
Created June 6, 2018 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmjatlanta/9c17db807cca638ef9158467592b8bf0 to your computer and use it in GitHub Desktop.
Save jmjatlanta/9c17db807cca638ef9158467592b8bf0 to your computer and use it in GitHub Desktop.
Test for asset::amount_to_string
/*
* Copyright (c) 2018 Bitshares Foundation, and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <boost/test/unit_test.hpp>
#include <string>
#include <cmath>
#include <graphene/chain/asset_object.hpp>
BOOST_AUTO_TEST_SUITE(asset_tests)
std::string convert_and_strip_zeros(long double val, int precision)
{
std::stringstream ss;
ss.precision(precision + 5);
ss << val;
return ss.str();
}
BOOST_AUTO_TEST_CASE( asset_to_from_string )
{
graphene::chain::asset_object obj1;
graphene::chain::share_type amt12345 = 12345;
BOOST_TEST_MESSAGE( "Testing positive numbers" );
for (int i = 0; i < 19; i++)
{
obj1.precision = i;
std::string expectedResult = convert_and_strip_zeros(amt12345.value * pow(10, i * -1), i);
BOOST_CHECK_EQUAL(expectedResult, obj1.amount_to_string(amt12345));
}
BOOST_TEST_MESSAGE( "Testing negative numbers" );
for (int i = 0; i < 19; i++)
{
obj1.precision = i;
std::string expectedResult = convert_and_strip_zeros(amt12345.value * pow(10, i * -1) * -1, i);
BOOST_CHECK_EQUAL(expectedResult, obj1.amount_to_string(amt12345 * -1));
}
}
BOOST_AUTO_TEST_SUITE_END()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment