...

Source file src/github.com/cockroachdb/apd/v3/decomposer_test.go

Documentation: github.com/cockroachdb/apd/v3

     1  // Copyright 2016 The Cockroach Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied. See the License for the specific language governing
    13  // permissions and limitations under the License.
    14  
    15  package apd
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  )
    21  
    22  func TestDecomposerRoundTrip(t *testing.T) {
    23  	list := []struct {
    24  		N string // Name.
    25  		S string // String value.
    26  		E bool   // Expect an error.
    27  	}{
    28  		{N: "Zero", S: "0"},
    29  		{N: "Normal-1", S: "123.456"},
    30  		{N: "Normal-2", S: "-123.456"},
    31  		{N: "NaN-1", S: "NaN"},
    32  		{N: "NaN-2", S: "-NaN"},
    33  		{N: "Infinity-1", S: "Infinity"},
    34  		{N: "Infinity-2", S: "-Infinity"},
    35  	}
    36  	for _, item := range list {
    37  		t.Run(item.N, func(t *testing.T) {
    38  			d, _, err := NewFromString(item.S)
    39  			if err != nil {
    40  				t.Fatal(err)
    41  			}
    42  			set, set2 := &Decimal{}, &Decimal{}
    43  			f, n, c, e := d.Decompose(nil)
    44  			err = set.Compose(f, n, c, e)
    45  			if err == nil && item.E {
    46  				t.Fatal("expected error, got <nil>")
    47  			}
    48  			err = set2.Compose(f, n, c, e)
    49  			if err == nil && item.E {
    50  				t.Fatal("expected error, got <nil>")
    51  			}
    52  			if set.Cmp(set2) != 0 {
    53  				t.Fatalf("composing the same value twice resulted in different values. set=%v set2=%v", set, set2)
    54  			}
    55  			if err != nil && !item.E {
    56  				t.Fatalf("unexpected error: %v", err)
    57  			}
    58  			if set.Cmp(d) != 0 {
    59  				t.Fatalf("values incorrect, got %v want %v (%s)", set, d, item.S)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestDecomposerCompose(t *testing.T) {
    66  	list := []struct {
    67  		N string // Name.
    68  		S string // String value.
    69  
    70  		Form byte // Form
    71  		Neg  bool
    72  		Coef []byte // Coefficent
    73  		Exp  int32
    74  
    75  		Err bool // Expect an error.
    76  	}{
    77  		{N: "Zero", S: "0", Coef: nil, Exp: 0},
    78  		{N: "Normal-1", S: "123.456", Coef: []byte{0x01, 0xE2, 0x40}, Exp: -3},
    79  		{N: "Neg-1", S: "-123.456", Neg: true, Coef: []byte{0x01, 0xE2, 0x40}, Exp: -3},
    80  		{N: "PosExp-1", S: "123456000", Coef: []byte{0x01, 0xE2, 0x40}, Exp: 3},
    81  		{N: "PosExp-2", S: "-123456000", Neg: true, Coef: []byte{0x01, 0xE2, 0x40}, Exp: 3},
    82  		{N: "AllDec-1", S: "0.123456", Coef: []byte{0x01, 0xE2, 0x40}, Exp: -6},
    83  		{N: "AllDec-2", S: "-0.123456", Neg: true, Coef: []byte{0x01, 0xE2, 0x40}, Exp: -6},
    84  		{N: "NaN-1", S: "NaN", Form: 2},
    85  		{N: "NaN-2", S: "-NaN", Form: 2, Neg: true},
    86  		{N: "Infinity-1", S: "Infinity", Form: 1},
    87  		{N: "Infinity-2", S: "-Infinity", Form: 1, Neg: true},
    88  	}
    89  
    90  	for _, item := range list {
    91  		t.Run(item.N, func(t *testing.T) {
    92  			d, _, err := NewFromString(item.S)
    93  			if err != nil {
    94  				t.Fatal(err)
    95  			}
    96  			err = d.Compose(item.Form, item.Neg, item.Coef, item.Exp)
    97  			if err != nil && !item.Err {
    98  				t.Fatalf("unexpected error, got %v", err)
    99  			}
   100  			if item.Err {
   101  				if err == nil {
   102  					t.Fatal("expected error, got <nil>")
   103  				}
   104  				return
   105  			}
   106  			if s := fmt.Sprintf("%f", d); s != item.S {
   107  				t.Fatalf("unexpected value, got %q want %q", s, item.S)
   108  			}
   109  		})
   110  	}
   111  }
   112  

View as plain text