github.com/goplus/igop@v0.25.0/constant/constant_test.go (about)

     1  /*
     2   * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package constant
    18  
    19  import (
    20  	"go/constant"
    21  	"go/token"
    22  	"testing"
    23  )
    24  
    25  func TestExactConstant(t *testing.T) {
    26  	const c = "3.14159265358979323846264338327950288419716939937510582097494459"
    27  	x := constant.MakeFromLiteral(c, token.FLOAT, 0)
    28  	if v := ExactConstant(x); v != c {
    29  		t.Fatalf(v)
    30  	}
    31  }
    32  
    33  func TestExactConstantEx1(t *testing.T) {
    34  	const c = "3.14159265358979323846264338327950288419716939937510582097494459"
    35  	x := constant.MakeFromLiteral(c, token.FLOAT, 0)
    36  	if v, exact := ExactConstantEx(x, true); v != c || exact != true {
    37  		t.Fatalf(v)
    38  	}
    39  }
    40  
    41  func TestExactConstantEx2(t *testing.T) {
    42  	const c = "1.401298464324817e-45"
    43  	x := constant.BinaryOp(constant.MakeFromLiteral("1", token.INT, 0), token.QUO, constant.MakeFromLiteral("713623846352979940529142984724747568191373312", token.INT, 0))
    44  	if v, exact := ExactConstantEx(x, true); v != c || exact != false {
    45  		t.Fatalf(v)
    46  	}
    47  }
    48  
    49  func TestExactConstantEx3(t *testing.T) {
    50  	const c = "1.01"
    51  	x := constant.BinaryOp(constant.MakeFromLiteral("101", token.INT, 0), token.QUO, constant.MakeFromLiteral("100", token.INT, 0))
    52  	if v, exact := ExactConstantEx(x, true); v != c || exact != true {
    53  		t.Fatalf(v)
    54  	}
    55  }
    56  
    57  func TestExactConstantEx4(t *testing.T) {
    58  	const c = "3.14e+100"
    59  	x := constant.MakeFromLiteral("3.14e100", token.FLOAT, 0)
    60  	if v, exact := ExactConstantEx(x, true); v != c || exact != true {
    61  		t.Fatalf(v)
    62  	}
    63  }
    64  
    65  func TestExactConstantEx5(t *testing.T) {
    66  	const c = "(11/10 + 5i)"
    67  	x := constant.BinaryOp(constant.MakeFromLiteral("1.1", token.FLOAT, 0), token.ADD, constant.MakeImag(constant.MakeInt64(5)))
    68  	if v, exact := ExactConstantEx(x, true); v != c || exact != true {
    69  		t.Fatalf(v)
    70  	}
    71  }
    72  
    73  func TestExactConstantEx6(t *testing.T) {
    74  	const c = "100000"
    75  	x := constant.MakeFromLiteral("100000.0", token.FLOAT, 0)
    76  	if v, exact := ExactConstantEx(x, true); v != c || exact != true {
    77  		t.Fatalf(v)
    78  	}
    79  }
    80  
    81  func TestExactConstantEx7(t *testing.T) {
    82  	const c = "1.2e+6"
    83  	x := constant.MakeFromLiteral("1200000.0", token.FLOAT, 0)
    84  	if v, exact := ExactConstantEx(x, true); v != c || exact != true {
    85  		t.Fatalf(v)
    86  	}
    87  }
    88  
    89  func TestExactConstantEx8(t *testing.T) {
    90  	const c = "1e-5"
    91  	x := constant.MakeFromLiteral("0.00001", token.FLOAT, 0)
    92  	if v, exact := ExactConstantEx(x, true); v != c || exact != true {
    93  		t.Fatalf(v)
    94  	}
    95  }
    96  
    97  func TestExactConstantEx9(t *testing.T) {
    98  	const c = "0.0001"
    99  	x := constant.MakeFromLiteral("0.0001", token.FLOAT, 0)
   100  	if v, exact := ExactConstantEx(x, true); v != c || exact != true {
   101  		t.Fatalf(v)
   102  	}
   103  }
   104  
   105  func TestExactConstantEx10(t *testing.T) {
   106  	const c = "0.1"
   107  	x := constant.MakeFromLiteral("0.1", token.FLOAT, 0)
   108  	if v, exact := ExactConstantEx(x, true); v != c || exact != true {
   109  		t.Fatalf(v)
   110  	}
   111  }