File: jsonfloat_test.go

package info (click to toggle)
golang-google-api 0.61.0-6
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 209,156 kB
  • sloc: sh: 183; makefile: 22; python: 4
file content (43 lines) | stat: -rw-r--r-- 961 bytes parent folder | download | duplicates (5)
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
// Copyright 2016 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gensupport

import (
	"encoding/json"
	"math"
	"testing"
)

func TestJSONFloat(t *testing.T) {
	for _, test := range []struct {
		in   string
		want float64
	}{
		{"0", 0},
		{"-10", -10},
		{"1e23", 1e23},
		{`"Infinity"`, math.Inf(1)},
		{`"-Infinity"`, math.Inf(-1)},
		{`"NaN"`, math.NaN()},
	} {
		var f64 JSONFloat64
		if err := json.Unmarshal([]byte(test.in), &f64); err != nil {
			t.Fatal(err)
		}
		got := float64(f64)
		if got != test.want && math.IsNaN(got) != math.IsNaN(test.want) {
			t.Errorf("%s: got %f, want %f", test.in, got, test.want)
		}
	}
}

func TestJSONFloatErrors(t *testing.T) {
	var f64 JSONFloat64
	for _, in := range []string{"", "a", `"Inf"`, `"-Inf"`, `"nan"`, `"nana"`} {
		if err := json.Unmarshal([]byte(in), &f64); err == nil {
			t.Errorf("%q: got nil, want error", in)
		}
	}
}