12e5b6d6dSopenharmony_ci// Copyright 2006-2008 the V8 project authors. All rights reserved. 22e5b6d6dSopenharmony_ci// Redistribution and use in source and binary forms, with or without 32e5b6d6dSopenharmony_ci// modification, are permitted provided that the following conditions are 42e5b6d6dSopenharmony_ci// met: 52e5b6d6dSopenharmony_ci// 62e5b6d6dSopenharmony_ci// * Redistributions of source code must retain the above copyright 72e5b6d6dSopenharmony_ci// notice, this list of conditions and the following disclaimer. 82e5b6d6dSopenharmony_ci// * Redistributions in binary form must reproduce the above 92e5b6d6dSopenharmony_ci// copyright notice, this list of conditions and the following 102e5b6d6dSopenharmony_ci// disclaimer in the documentation and/or other materials provided 112e5b6d6dSopenharmony_ci// with the distribution. 122e5b6d6dSopenharmony_ci// * Neither the name of Google Inc. nor the names of its 132e5b6d6dSopenharmony_ci// contributors may be used to endorse or promote products derived 142e5b6d6dSopenharmony_ci// from this software without specific prior written permission. 152e5b6d6dSopenharmony_ci// 162e5b6d6dSopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172e5b6d6dSopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182e5b6d6dSopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192e5b6d6dSopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202e5b6d6dSopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212e5b6d6dSopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222e5b6d6dSopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232e5b6d6dSopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242e5b6d6dSopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252e5b6d6dSopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262e5b6d6dSopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272e5b6d6dSopenharmony_ci 282e5b6d6dSopenharmony_ci#include <stdlib.h> 292e5b6d6dSopenharmony_ci 302e5b6d6dSopenharmony_ci#include "cctest.h" 312e5b6d6dSopenharmony_ci#include "double-conversion/diy-fp.h" 322e5b6d6dSopenharmony_ci#include "double-conversion/fast-dtoa.h" 332e5b6d6dSopenharmony_ci#include "gay-precision.h" 342e5b6d6dSopenharmony_ci#include "gay-shortest.h" 352e5b6d6dSopenharmony_ci#include "gay-shortest-single.h" 362e5b6d6dSopenharmony_ci#include "double-conversion/ieee.h" 372e5b6d6dSopenharmony_ci#include "double-conversion/utils.h" 382e5b6d6dSopenharmony_ci 392e5b6d6dSopenharmony_ciusing namespace double_conversion; 402e5b6d6dSopenharmony_ci 412e5b6d6dSopenharmony_cistatic const int kBufferSize = 100; 422e5b6d6dSopenharmony_ci 432e5b6d6dSopenharmony_ci 442e5b6d6dSopenharmony_ci// Removes trailing '0' digits. 452e5b6d6dSopenharmony_cistatic void TrimRepresentation(Vector<char> representation) { 462e5b6d6dSopenharmony_ci int len = static_cast<int>(strlen(representation.start())); 472e5b6d6dSopenharmony_ci int i; 482e5b6d6dSopenharmony_ci for (i = len - 1; i >= 0; --i) { 492e5b6d6dSopenharmony_ci if (representation[i] != '0') break; 502e5b6d6dSopenharmony_ci } 512e5b6d6dSopenharmony_ci representation[i + 1] = '\0'; 522e5b6d6dSopenharmony_ci} 532e5b6d6dSopenharmony_ci 542e5b6d6dSopenharmony_ci 552e5b6d6dSopenharmony_ciTEST(FastDtoaShortestVariousDoubles) { 562e5b6d6dSopenharmony_ci char buffer_container[kBufferSize]; 572e5b6d6dSopenharmony_ci Vector<char> buffer(buffer_container, kBufferSize); 582e5b6d6dSopenharmony_ci int length; 592e5b6d6dSopenharmony_ci int point; 602e5b6d6dSopenharmony_ci bool status; 612e5b6d6dSopenharmony_ci 622e5b6d6dSopenharmony_ci double min_double = 5e-324; 632e5b6d6dSopenharmony_ci status = FastDtoa(min_double, FAST_DTOA_SHORTEST, 0, 642e5b6d6dSopenharmony_ci buffer, &length, &point); 652e5b6d6dSopenharmony_ci CHECK(status); 662e5b6d6dSopenharmony_ci CHECK_EQ("5", buffer.start()); 672e5b6d6dSopenharmony_ci CHECK_EQ(-323, point); 682e5b6d6dSopenharmony_ci 692e5b6d6dSopenharmony_ci double max_double = 1.7976931348623157e308; 702e5b6d6dSopenharmony_ci status = FastDtoa(max_double, FAST_DTOA_SHORTEST, 0, 712e5b6d6dSopenharmony_ci buffer, &length, &point); 722e5b6d6dSopenharmony_ci CHECK(status); 732e5b6d6dSopenharmony_ci CHECK_EQ("17976931348623157", buffer.start()); 742e5b6d6dSopenharmony_ci CHECK_EQ(309, point); 752e5b6d6dSopenharmony_ci 762e5b6d6dSopenharmony_ci status = FastDtoa(4294967272.0, FAST_DTOA_SHORTEST, 0, 772e5b6d6dSopenharmony_ci buffer, &length, &point); 782e5b6d6dSopenharmony_ci CHECK(status); 792e5b6d6dSopenharmony_ci CHECK_EQ("4294967272", buffer.start()); 802e5b6d6dSopenharmony_ci CHECK_EQ(10, point); 812e5b6d6dSopenharmony_ci 822e5b6d6dSopenharmony_ci status = FastDtoa(4.1855804968213567e298, FAST_DTOA_SHORTEST, 0, 832e5b6d6dSopenharmony_ci buffer, &length, &point); 842e5b6d6dSopenharmony_ci CHECK(status); 852e5b6d6dSopenharmony_ci CHECK_EQ("4185580496821357", buffer.start()); 862e5b6d6dSopenharmony_ci CHECK_EQ(299, point); 872e5b6d6dSopenharmony_ci 882e5b6d6dSopenharmony_ci status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_SHORTEST, 0, 892e5b6d6dSopenharmony_ci buffer, &length, &point); 902e5b6d6dSopenharmony_ci CHECK(status); 912e5b6d6dSopenharmony_ci CHECK_EQ("5562684646268003", buffer.start()); 922e5b6d6dSopenharmony_ci CHECK_EQ(-308, point); 932e5b6d6dSopenharmony_ci 942e5b6d6dSopenharmony_ci status = FastDtoa(2147483648.0, FAST_DTOA_SHORTEST, 0, 952e5b6d6dSopenharmony_ci buffer, &length, &point); 962e5b6d6dSopenharmony_ci CHECK(status); 972e5b6d6dSopenharmony_ci CHECK_EQ("2147483648", buffer.start()); 982e5b6d6dSopenharmony_ci CHECK_EQ(10, point); 992e5b6d6dSopenharmony_ci 1002e5b6d6dSopenharmony_ci status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_SHORTEST, 0, 1012e5b6d6dSopenharmony_ci buffer, &length, &point); 1022e5b6d6dSopenharmony_ci if (status) { // Not all FastDtoa variants manage to compute this number. 1032e5b6d6dSopenharmony_ci CHECK_EQ("35844466002796428", buffer.start()); 1042e5b6d6dSopenharmony_ci CHECK_EQ(299, point); 1052e5b6d6dSopenharmony_ci } 1062e5b6d6dSopenharmony_ci 1072e5b6d6dSopenharmony_ci uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000); 1082e5b6d6dSopenharmony_ci double v = Double(smallest_normal64).value(); 1092e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point); 1102e5b6d6dSopenharmony_ci if (status) { 1112e5b6d6dSopenharmony_ci CHECK_EQ("22250738585072014", buffer.start()); 1122e5b6d6dSopenharmony_ci CHECK_EQ(-307, point); 1132e5b6d6dSopenharmony_ci } 1142e5b6d6dSopenharmony_ci 1152e5b6d6dSopenharmony_ci uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF); 1162e5b6d6dSopenharmony_ci v = Double(largest_denormal64).value(); 1172e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point); 1182e5b6d6dSopenharmony_ci if (status) { 1192e5b6d6dSopenharmony_ci CHECK_EQ("2225073858507201", buffer.start()); 1202e5b6d6dSopenharmony_ci CHECK_EQ(-307, point); 1212e5b6d6dSopenharmony_ci } 1222e5b6d6dSopenharmony_ci} 1232e5b6d6dSopenharmony_ci 1242e5b6d6dSopenharmony_ci 1252e5b6d6dSopenharmony_ciTEST(FastDtoaShortestVariousFloats) { 1262e5b6d6dSopenharmony_ci char buffer_container[kBufferSize]; 1272e5b6d6dSopenharmony_ci Vector<char> buffer(buffer_container, kBufferSize); 1282e5b6d6dSopenharmony_ci int length; 1292e5b6d6dSopenharmony_ci int point; 1302e5b6d6dSopenharmony_ci bool status; 1312e5b6d6dSopenharmony_ci 1322e5b6d6dSopenharmony_ci float min_float = 1e-45f; 1332e5b6d6dSopenharmony_ci status = FastDtoa(min_float, FAST_DTOA_SHORTEST_SINGLE, 0, 1342e5b6d6dSopenharmony_ci buffer, &length, &point); 1352e5b6d6dSopenharmony_ci CHECK(status); 1362e5b6d6dSopenharmony_ci CHECK_EQ("1", buffer.start()); 1372e5b6d6dSopenharmony_ci CHECK_EQ(-44, point); 1382e5b6d6dSopenharmony_ci 1392e5b6d6dSopenharmony_ci 1402e5b6d6dSopenharmony_ci float max_float = 3.4028234e38f; 1412e5b6d6dSopenharmony_ci status = FastDtoa(max_float, FAST_DTOA_SHORTEST_SINGLE, 0, 1422e5b6d6dSopenharmony_ci buffer, &length, &point); 1432e5b6d6dSopenharmony_ci CHECK(status); 1442e5b6d6dSopenharmony_ci CHECK_EQ("34028235", buffer.start()); 1452e5b6d6dSopenharmony_ci CHECK_EQ(39, point); 1462e5b6d6dSopenharmony_ci 1472e5b6d6dSopenharmony_ci status = FastDtoa(4294967272.0f, FAST_DTOA_SHORTEST_SINGLE, 0, 1482e5b6d6dSopenharmony_ci buffer, &length, &point); 1492e5b6d6dSopenharmony_ci CHECK(status); 1502e5b6d6dSopenharmony_ci CHECK_EQ("42949673", buffer.start()); 1512e5b6d6dSopenharmony_ci CHECK_EQ(10, point); 1522e5b6d6dSopenharmony_ci 1532e5b6d6dSopenharmony_ci status = FastDtoa(3.32306998946228968226e+35f, FAST_DTOA_SHORTEST_SINGLE, 0, 1542e5b6d6dSopenharmony_ci buffer, &length, &point); 1552e5b6d6dSopenharmony_ci CHECK(status); 1562e5b6d6dSopenharmony_ci CHECK_EQ("332307", buffer.start()); 1572e5b6d6dSopenharmony_ci CHECK_EQ(36, point); 1582e5b6d6dSopenharmony_ci 1592e5b6d6dSopenharmony_ci status = FastDtoa(1.2341e-41f, FAST_DTOA_SHORTEST_SINGLE, 0, 1602e5b6d6dSopenharmony_ci buffer, &length, &point); 1612e5b6d6dSopenharmony_ci CHECK(status); 1622e5b6d6dSopenharmony_ci CHECK_EQ("12341", buffer.start()); 1632e5b6d6dSopenharmony_ci CHECK_EQ(-40, point); 1642e5b6d6dSopenharmony_ci 1652e5b6d6dSopenharmony_ci status = FastDtoa(3.3554432e7, FAST_DTOA_SHORTEST_SINGLE, 0, 1662e5b6d6dSopenharmony_ci buffer, &length, &point); 1672e5b6d6dSopenharmony_ci CHECK(status); 1682e5b6d6dSopenharmony_ci CHECK_EQ("33554432", buffer.start()); 1692e5b6d6dSopenharmony_ci CHECK_EQ(8, point); 1702e5b6d6dSopenharmony_ci 1712e5b6d6dSopenharmony_ci status = FastDtoa(3.26494756798464e14f, FAST_DTOA_SHORTEST_SINGLE, 0, 1722e5b6d6dSopenharmony_ci buffer, &length, &point); 1732e5b6d6dSopenharmony_ci CHECK(status); 1742e5b6d6dSopenharmony_ci CHECK_EQ("32649476", buffer.start()); 1752e5b6d6dSopenharmony_ci CHECK_EQ(15, point); 1762e5b6d6dSopenharmony_ci 1772e5b6d6dSopenharmony_ci status = FastDtoa(3.91132223637771935344e37f, FAST_DTOA_SHORTEST_SINGLE, 0, 1782e5b6d6dSopenharmony_ci buffer, &length, &point); 1792e5b6d6dSopenharmony_ci if (status) { // Not all FastDtoa variants manage to compute this number. 1802e5b6d6dSopenharmony_ci CHECK_EQ("39113222", buffer.start()); 1812e5b6d6dSopenharmony_ci CHECK_EQ(38, point); 1822e5b6d6dSopenharmony_ci } 1832e5b6d6dSopenharmony_ci 1842e5b6d6dSopenharmony_ci uint32_t smallest_normal32 = 0x00800000; 1852e5b6d6dSopenharmony_ci float v = Single(smallest_normal32).value(); 1862e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0, buffer, &length, &point); 1872e5b6d6dSopenharmony_ci if (status) { 1882e5b6d6dSopenharmony_ci CHECK_EQ("11754944", buffer.start()); 1892e5b6d6dSopenharmony_ci CHECK_EQ(-37, point); 1902e5b6d6dSopenharmony_ci } 1912e5b6d6dSopenharmony_ci 1922e5b6d6dSopenharmony_ci uint32_t largest_denormal32 = 0x007FFFFF; 1932e5b6d6dSopenharmony_ci v = Single(largest_denormal32).value(); 1942e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0, buffer, &length, &point); 1952e5b6d6dSopenharmony_ci CHECK(status); 1962e5b6d6dSopenharmony_ci CHECK_EQ("11754942", buffer.start()); 1972e5b6d6dSopenharmony_ci CHECK_EQ(-37, point); 1982e5b6d6dSopenharmony_ci} 1992e5b6d6dSopenharmony_ci 2002e5b6d6dSopenharmony_ci 2012e5b6d6dSopenharmony_ciTEST(FastDtoaPrecisionVariousDoubles) { 2022e5b6d6dSopenharmony_ci char buffer_container[kBufferSize]; 2032e5b6d6dSopenharmony_ci Vector<char> buffer(buffer_container, kBufferSize); 2042e5b6d6dSopenharmony_ci int length; 2052e5b6d6dSopenharmony_ci int point; 2062e5b6d6dSopenharmony_ci bool status; 2072e5b6d6dSopenharmony_ci 2082e5b6d6dSopenharmony_ci status = FastDtoa(1.0, FAST_DTOA_PRECISION, 3, buffer, &length, &point); 2092e5b6d6dSopenharmony_ci CHECK(status); 2102e5b6d6dSopenharmony_ci CHECK(3 >= length); 2112e5b6d6dSopenharmony_ci TrimRepresentation(buffer); 2122e5b6d6dSopenharmony_ci CHECK_EQ("1", buffer.start()); 2132e5b6d6dSopenharmony_ci CHECK_EQ(1, point); 2142e5b6d6dSopenharmony_ci 2152e5b6d6dSopenharmony_ci status = FastDtoa(1.5, FAST_DTOA_PRECISION, 10, buffer, &length, &point); 2162e5b6d6dSopenharmony_ci if (status) { 2172e5b6d6dSopenharmony_ci CHECK(10 >= length); 2182e5b6d6dSopenharmony_ci TrimRepresentation(buffer); 2192e5b6d6dSopenharmony_ci CHECK_EQ("15", buffer.start()); 2202e5b6d6dSopenharmony_ci CHECK_EQ(1, point); 2212e5b6d6dSopenharmony_ci } 2222e5b6d6dSopenharmony_ci 2232e5b6d6dSopenharmony_ci double min_double = 5e-324; 2242e5b6d6dSopenharmony_ci status = FastDtoa(min_double, FAST_DTOA_PRECISION, 5, 2252e5b6d6dSopenharmony_ci buffer, &length, &point); 2262e5b6d6dSopenharmony_ci CHECK(status); 2272e5b6d6dSopenharmony_ci CHECK_EQ("49407", buffer.start()); 2282e5b6d6dSopenharmony_ci CHECK_EQ(-323, point); 2292e5b6d6dSopenharmony_ci 2302e5b6d6dSopenharmony_ci double max_double = 1.7976931348623157e308; 2312e5b6d6dSopenharmony_ci status = FastDtoa(max_double, FAST_DTOA_PRECISION, 7, 2322e5b6d6dSopenharmony_ci buffer, &length, &point); 2332e5b6d6dSopenharmony_ci CHECK(status); 2342e5b6d6dSopenharmony_ci CHECK_EQ("1797693", buffer.start()); 2352e5b6d6dSopenharmony_ci CHECK_EQ(309, point); 2362e5b6d6dSopenharmony_ci 2372e5b6d6dSopenharmony_ci status = FastDtoa(4294967272.0, FAST_DTOA_PRECISION, 14, 2382e5b6d6dSopenharmony_ci buffer, &length, &point); 2392e5b6d6dSopenharmony_ci if (status) { 2402e5b6d6dSopenharmony_ci CHECK(14 >= length); 2412e5b6d6dSopenharmony_ci TrimRepresentation(buffer); 2422e5b6d6dSopenharmony_ci CHECK_EQ("4294967272", buffer.start()); 2432e5b6d6dSopenharmony_ci CHECK_EQ(10, point); 2442e5b6d6dSopenharmony_ci } 2452e5b6d6dSopenharmony_ci 2462e5b6d6dSopenharmony_ci status = FastDtoa(4.1855804968213567e298, FAST_DTOA_PRECISION, 17, 2472e5b6d6dSopenharmony_ci buffer, &length, &point); 2482e5b6d6dSopenharmony_ci CHECK(status); 2492e5b6d6dSopenharmony_ci CHECK_EQ("41855804968213567", buffer.start()); 2502e5b6d6dSopenharmony_ci CHECK_EQ(299, point); 2512e5b6d6dSopenharmony_ci 2522e5b6d6dSopenharmony_ci status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_PRECISION, 1, 2532e5b6d6dSopenharmony_ci buffer, &length, &point); 2542e5b6d6dSopenharmony_ci CHECK(status); 2552e5b6d6dSopenharmony_ci CHECK_EQ("6", buffer.start()); 2562e5b6d6dSopenharmony_ci CHECK_EQ(-308, point); 2572e5b6d6dSopenharmony_ci 2582e5b6d6dSopenharmony_ci status = FastDtoa(2147483648.0, FAST_DTOA_PRECISION, 5, 2592e5b6d6dSopenharmony_ci buffer, &length, &point); 2602e5b6d6dSopenharmony_ci CHECK(status); 2612e5b6d6dSopenharmony_ci CHECK_EQ("21475", buffer.start()); 2622e5b6d6dSopenharmony_ci CHECK_EQ(10, point); 2632e5b6d6dSopenharmony_ci 2642e5b6d6dSopenharmony_ci status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_PRECISION, 10, 2652e5b6d6dSopenharmony_ci buffer, &length, &point); 2662e5b6d6dSopenharmony_ci CHECK(status); 2672e5b6d6dSopenharmony_ci CHECK(10 >= length); 2682e5b6d6dSopenharmony_ci TrimRepresentation(buffer); 2692e5b6d6dSopenharmony_ci CHECK_EQ("35844466", buffer.start()); 2702e5b6d6dSopenharmony_ci CHECK_EQ(299, point); 2712e5b6d6dSopenharmony_ci 2722e5b6d6dSopenharmony_ci uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000); 2732e5b6d6dSopenharmony_ci double v = Double(smallest_normal64).value(); 2742e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point); 2752e5b6d6dSopenharmony_ci CHECK(status); 2762e5b6d6dSopenharmony_ci CHECK_EQ("22250738585072014", buffer.start()); 2772e5b6d6dSopenharmony_ci CHECK_EQ(-307, point); 2782e5b6d6dSopenharmony_ci 2792e5b6d6dSopenharmony_ci uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF); 2802e5b6d6dSopenharmony_ci v = Double(largest_denormal64).value(); 2812e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point); 2822e5b6d6dSopenharmony_ci CHECK(status); 2832e5b6d6dSopenharmony_ci CHECK(20 >= length); 2842e5b6d6dSopenharmony_ci TrimRepresentation(buffer); 2852e5b6d6dSopenharmony_ci CHECK_EQ("22250738585072009", buffer.start()); 2862e5b6d6dSopenharmony_ci CHECK_EQ(-307, point); 2872e5b6d6dSopenharmony_ci 2882e5b6d6dSopenharmony_ci v = 3.3161339052167390562200598e-237; 2892e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_PRECISION, 18, buffer, &length, &point); 2902e5b6d6dSopenharmony_ci CHECK(status); 2912e5b6d6dSopenharmony_ci CHECK_EQ("331613390521673906", buffer.start()); 2922e5b6d6dSopenharmony_ci CHECK_EQ(-236, point); 2932e5b6d6dSopenharmony_ci 2942e5b6d6dSopenharmony_ci v = 7.9885183916008099497815232e+191; 2952e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_PRECISION, 4, buffer, &length, &point); 2962e5b6d6dSopenharmony_ci CHECK(status); 2972e5b6d6dSopenharmony_ci CHECK_EQ("7989", buffer.start()); 2982e5b6d6dSopenharmony_ci CHECK_EQ(192, point); 2992e5b6d6dSopenharmony_ci} 3002e5b6d6dSopenharmony_ci 3012e5b6d6dSopenharmony_ci 3022e5b6d6dSopenharmony_ciTEST(FastDtoaGayShortest) { 3032e5b6d6dSopenharmony_ci char buffer_container[kBufferSize]; 3042e5b6d6dSopenharmony_ci Vector<char> buffer(buffer_container, kBufferSize); 3052e5b6d6dSopenharmony_ci bool status; 3062e5b6d6dSopenharmony_ci int length; 3072e5b6d6dSopenharmony_ci int point; 3082e5b6d6dSopenharmony_ci int succeeded = 0; 3092e5b6d6dSopenharmony_ci int total = 0; 3102e5b6d6dSopenharmony_ci bool needed_max_length = false; 3112e5b6d6dSopenharmony_ci 3122e5b6d6dSopenharmony_ci Vector<const PrecomputedShortest> precomputed = 3132e5b6d6dSopenharmony_ci PrecomputedShortestRepresentations(); 3142e5b6d6dSopenharmony_ci for (int i = 0; i < precomputed.length(); ++i) { 3152e5b6d6dSopenharmony_ci const PrecomputedShortest current_test = precomputed[i]; 3162e5b6d6dSopenharmony_ci total++; 3172e5b6d6dSopenharmony_ci double v = current_test.v; 3182e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point); 3192e5b6d6dSopenharmony_ci CHECK(kFastDtoaMaximalLength >= length); 3202e5b6d6dSopenharmony_ci if (!status) continue; 3212e5b6d6dSopenharmony_ci if (length == kFastDtoaMaximalLength) needed_max_length = true; 3222e5b6d6dSopenharmony_ci succeeded++; 3232e5b6d6dSopenharmony_ci CHECK_EQ(current_test.decimal_point, point); 3242e5b6d6dSopenharmony_ci CHECK_EQ(current_test.representation, buffer.start()); 3252e5b6d6dSopenharmony_ci } 3262e5b6d6dSopenharmony_ci CHECK(succeeded*1.0/total > 0.99); 3272e5b6d6dSopenharmony_ci CHECK(needed_max_length); 3282e5b6d6dSopenharmony_ci} 3292e5b6d6dSopenharmony_ci 3302e5b6d6dSopenharmony_ci 3312e5b6d6dSopenharmony_ciTEST(FastDtoaGayShortestSingle) { 3322e5b6d6dSopenharmony_ci char buffer_container[kBufferSize]; 3332e5b6d6dSopenharmony_ci Vector<char> buffer(buffer_container, kBufferSize); 3342e5b6d6dSopenharmony_ci bool status; 3352e5b6d6dSopenharmony_ci int length; 3362e5b6d6dSopenharmony_ci int point; 3372e5b6d6dSopenharmony_ci int succeeded = 0; 3382e5b6d6dSopenharmony_ci int total = 0; 3392e5b6d6dSopenharmony_ci bool needed_max_length = false; 3402e5b6d6dSopenharmony_ci 3412e5b6d6dSopenharmony_ci Vector<const PrecomputedShortestSingle> precomputed = 3422e5b6d6dSopenharmony_ci PrecomputedShortestSingleRepresentations(); 3432e5b6d6dSopenharmony_ci for (int i = 0; i < precomputed.length(); ++i) { 3442e5b6d6dSopenharmony_ci const PrecomputedShortestSingle current_test = precomputed[i]; 3452e5b6d6dSopenharmony_ci total++; 3462e5b6d6dSopenharmony_ci float v = current_test.v; 3472e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0, buffer, &length, &point); 3482e5b6d6dSopenharmony_ci CHECK(kFastDtoaMaximalSingleLength >= length); 3492e5b6d6dSopenharmony_ci if (!status) continue; 3502e5b6d6dSopenharmony_ci if (length == kFastDtoaMaximalSingleLength) needed_max_length = true; 3512e5b6d6dSopenharmony_ci succeeded++; 3522e5b6d6dSopenharmony_ci CHECK_EQ(current_test.decimal_point, point); 3532e5b6d6dSopenharmony_ci CHECK_EQ(current_test.representation, buffer.start()); 3542e5b6d6dSopenharmony_ci } 3552e5b6d6dSopenharmony_ci CHECK(succeeded*1.0/total > 0.98); 3562e5b6d6dSopenharmony_ci CHECK(needed_max_length); 3572e5b6d6dSopenharmony_ci} 3582e5b6d6dSopenharmony_ci 3592e5b6d6dSopenharmony_ci 3602e5b6d6dSopenharmony_ciTEST(FastDtoaGayPrecision) { 3612e5b6d6dSopenharmony_ci char buffer_container[kBufferSize]; 3622e5b6d6dSopenharmony_ci Vector<char> buffer(buffer_container, kBufferSize); 3632e5b6d6dSopenharmony_ci bool status; 3642e5b6d6dSopenharmony_ci int length; 3652e5b6d6dSopenharmony_ci int point; 3662e5b6d6dSopenharmony_ci int succeeded = 0; 3672e5b6d6dSopenharmony_ci int total = 0; 3682e5b6d6dSopenharmony_ci // Count separately for entries with less than 15 requested digits. 3692e5b6d6dSopenharmony_ci int succeeded_15 = 0; 3702e5b6d6dSopenharmony_ci int total_15 = 0; 3712e5b6d6dSopenharmony_ci 3722e5b6d6dSopenharmony_ci Vector<const PrecomputedPrecision> precomputed = 3732e5b6d6dSopenharmony_ci PrecomputedPrecisionRepresentations(); 3742e5b6d6dSopenharmony_ci for (int i = 0; i < precomputed.length(); ++i) { 3752e5b6d6dSopenharmony_ci const PrecomputedPrecision current_test = precomputed[i]; 3762e5b6d6dSopenharmony_ci double v = current_test.v; 3772e5b6d6dSopenharmony_ci int number_digits = current_test.number_digits; 3782e5b6d6dSopenharmony_ci total++; 3792e5b6d6dSopenharmony_ci if (number_digits <= 15) total_15++; 3802e5b6d6dSopenharmony_ci status = FastDtoa(v, FAST_DTOA_PRECISION, number_digits, 3812e5b6d6dSopenharmony_ci buffer, &length, &point); 3822e5b6d6dSopenharmony_ci CHECK(number_digits >= length); 3832e5b6d6dSopenharmony_ci if (!status) continue; 3842e5b6d6dSopenharmony_ci succeeded++; 3852e5b6d6dSopenharmony_ci if (number_digits <= 15) succeeded_15++; 3862e5b6d6dSopenharmony_ci TrimRepresentation(buffer); 3872e5b6d6dSopenharmony_ci CHECK_EQ(current_test.decimal_point, point); 3882e5b6d6dSopenharmony_ci CHECK_EQ(current_test.representation, buffer.start()); 3892e5b6d6dSopenharmony_ci } 3902e5b6d6dSopenharmony_ci // The precomputed numbers contain many entries with many requested 3912e5b6d6dSopenharmony_ci // digits. These have a high failure rate and we therefore expect a lower 3922e5b6d6dSopenharmony_ci // success rate than for the shortest representation. 3932e5b6d6dSopenharmony_ci CHECK(succeeded*1.0/total > 0.85); 3942e5b6d6dSopenharmony_ci // However with less than 15 digits almost the algorithm should almost always 3952e5b6d6dSopenharmony_ci // succeed. 3962e5b6d6dSopenharmony_ci CHECK(succeeded_15*1.0/total_15 > 0.9999); 3972e5b6d6dSopenharmony_ci} 398