github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/common/overflow/overflow_test.go (about)

     1  package overflow
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  )
     7  import "fmt"
     8  
     9  // sample all possibilities of 8 bit numbers
    10  // by checking against 64 bit numbers
    11  
    12  func TestAlgorithms(t *testing.T) {
    13  
    14  	errors := 0
    15  
    16  	for a64 := int64(math.MinInt8); a64 <= int64(math.MaxInt8); a64++ {
    17  
    18  		for b64 := int64(math.MinInt8); b64 <= int64(math.MaxInt8) && errors < 10; b64++ {
    19  
    20  			a8 := int8(a64)
    21  			b8 := int8(b64)
    22  
    23  			if int64(a8) != a64 || int64(b8) != b64 {
    24  				t.Fatal("LOGIC FAILURE IN TEST")
    25  			}
    26  
    27  			// ADDITION
    28  			{
    29  				r64 := a64 + b64
    30  
    31  				// now the verification
    32  				result, ok := Add8(a8, b8)
    33  				if ok && int64(result) != r64 {
    34  					t.Errorf("failed to fail on %v + %v = %v instead of %v\n",
    35  						a8, b8, result, r64)
    36  					errors++
    37  				}
    38  				if !ok && int64(result) == r64 {
    39  					t.Fail()
    40  					errors++
    41  				}
    42  			}
    43  
    44  			// SUBTRACTION
    45  			{
    46  				r64 := a64 - b64
    47  
    48  				// now the verification
    49  				result, ok := Sub8(a8, b8)
    50  				if ok && int64(result) != r64 {
    51  					t.Errorf("failed to fail on %v - %v = %v instead of %v\n",
    52  						a8, b8, result, r64)
    53  				}
    54  				if !ok && int64(result) == r64 {
    55  					t.Fail()
    56  					errors++
    57  				}
    58  			}
    59  
    60  			// MULTIPLICATION
    61  			{
    62  				r64 := a64 * b64
    63  
    64  				// now the verification
    65  				result, ok := Mul8(a8, b8)
    66  				if ok && int64(result) != r64 {
    67  					t.Errorf("failed to fail on %v * %v = %v instead of %v\n",
    68  						a8, b8, result, r64)
    69  					errors++
    70  				}
    71  				if !ok && int64(result) == r64 {
    72  					t.Fail()
    73  					errors++
    74  				}
    75  			}
    76  
    77  			// DIVISION
    78  			if b8 != 0 {
    79  				r64 := a64 / b64
    80  
    81  				// now the verification
    82  				result, _, ok := Quotient8(a8, b8)
    83  				if ok && int64(result) != r64 {
    84  					t.Errorf("failed to fail on %v / %v = %v instead of %v\n",
    85  						a8, b8, result, r64)
    86  					errors++
    87  				}
    88  				if !ok && result != 0 && int64(result) == r64 {
    89  					t.Fail()
    90  					errors++
    91  				}
    92  			}
    93  		}
    94  	}
    95  
    96  }
    97  
    98  func TestQuotient(t *testing.T) {
    99  	q, r, ok := Quotient(100, 3)
   100  	if r != 1 || q != 33 || !ok {
   101  		t.Errorf("expected 100/3 => 33, r=1")
   102  	}
   103  	if _, _, ok = Quotient(1, 0); ok {
   104  		t.Error("unexpected lack of failure")
   105  	}
   106  }
   107  
   108  //func TestAdditionInt(t *testing.T) {
   109  //	fmt.Printf("\nminint8 = %v\n", math.MinInt8)
   110  //	fmt.Printf("maxint8 = %v\n\n", math.MaxInt8)
   111  //	fmt.Printf("maxint32 = %v\n", math.MaxInt32)
   112  //	fmt.Printf("minint32 = %v\n\n", math.MinInt32)
   113  //	fmt.Printf("maxint64 = %v\n", math.MaxInt64)
   114  //	fmt.Printf("minint64 = %v\n\n", math.MinInt64)
   115  //}
   116  
   117  func Test64(t *testing.T) {
   118  	fmt.Println("64bit:", _is64Bit())
   119  }