File: JSONNumericParsers_test.cpp

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (75 lines) | stat: -rw-r--r-- 2,299 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
 * (c) 2026 by Mega Limited, New Zealand
 *
 * This file is part of the MEGA SDK - Client Access Engine.
 *
 * Applications using the MEGA API must present a valid application key
 * and comply with the the rules set forth in the Terms of Service.
 *
 * The MEGA SDK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright Simplified (2-clause) BSD License.
 *
 * You should have received a copy of the license along with this
 * program.
 */

#include <gtest/gtest.h>

#include <mega.h>

TEST(JSONNumericParsers, GetFloatParsesDecimalNumbers)
{
    mega::JSON integerJson("25");
    mega::JSON decimalJson("25.99");
    mega::JSON negativeJson("-12.5");
    mega::JSON leadingDotJson(".5");

    EXPECT_DOUBLE_EQ(integerJson.getfloat(), 25.0);
    EXPECT_DOUBLE_EQ(decimalJson.getfloat(), 25.99);
    EXPECT_DOUBLE_EQ(negativeJson.getfloat(), -12.5);
    EXPECT_DOUBLE_EQ(leadingDotJson.getfloat(), 0.5);
}

TEST(JSONNumericParsers, GetFloatParsesScientificNotation)
{
    mega::JSON negativeExpJson("-3.5e1");
    mega::JSON positiveExpJson("2E3");
    mega::JSON fractionalExpJson("4.2e-2");

    EXPECT_DOUBLE_EQ(negativeExpJson.getfloat(), -35.0);
    EXPECT_DOUBLE_EQ(positiveExpJson.getfloat(), 2000.0);
    EXPECT_DOUBLE_EQ(fractionalExpJson.getfloat(), 0.042);
}

TEST(JSONNumericParsers, GetFloatAdvancesAcrossSequentialTokens)
{
    mega::JSON json("25.99,-3.5e1,2E3");

    EXPECT_DOUBLE_EQ(json.getfloat(), 25.99);
    EXPECT_DOUBLE_EQ(json.getfloat(), -35.0);
    EXPECT_DOUBLE_EQ(json.getfloat(), 2000.0);
}

TEST(JSONNumericParsers, GetFloatParsesValuesAfterNameValueSeparator)
{
    mega::JSON json(":25.99,-4.2e-1");

    EXPECT_DOUBLE_EQ(json.getfloat(), 25.99);
    EXPECT_DOUBLE_EQ(json.getfloat(), -0.42);
}

TEST(JSONNumericParsers, GetFloatRejectsMalformedValues)
{
    mega::JSON invalidStart("abc");
    mega::JSON invalidSign("-");
    mega::JSON invalidExponent("1e");
    mega::JSON invalidExponentSign("1e-");

    EXPECT_DOUBLE_EQ(invalidStart.getfloat(), -1.0);
    EXPECT_DOUBLE_EQ(invalidSign.getfloat(), 0.0);
    EXPECT_DOUBLE_EQ(invalidExponent.getfloat(), 1.0);
    EXPECT_DOUBLE_EQ(invalidExponentSign.getfloat(), 1.0);
}