github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/memo/cost_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package memo_test
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
    17  )
    18  
    19  func TestCostLess(t *testing.T) {
    20  	testCases := []struct {
    21  		left, right memo.Cost
    22  		expected    bool
    23  	}{
    24  		{0.0, 1.0, true},
    25  		{0.0, 1e-20, true},
    26  		{0.0, 0.0, false},
    27  		{1.0, 0.0, false},
    28  		{1e-20, 1.0000000000001e-20, false},
    29  		{1e-20, 1.000001e-20, true},
    30  		{1, 1.00000000000001, false},
    31  		{1, 1.00000001, true},
    32  		{1000, 1000.00000000001, false},
    33  		{1000, 1000.00001, true},
    34  	}
    35  	for _, tc := range testCases {
    36  		if tc.left.Less(tc.right) != tc.expected {
    37  			t.Errorf("expected %v.Less(%v) to be %v", tc.left, tc.right, tc.expected)
    38  		}
    39  	}
    40  }
    41  
    42  func TestCostSub(t *testing.T) {
    43  	testSub := func(left, right memo.Cost, expected memo.Cost) {
    44  		actual := left.Sub(right)
    45  		if actual != expected {
    46  			t.Errorf("expected %v.Sub(%v) to be %v, got %v", left, right, expected, actual)
    47  		}
    48  	}
    49  
    50  	testSub(memo.Cost(10.0), memo.Cost(3.0), memo.Cost(7.0))
    51  	testSub(memo.Cost(3.0), memo.Cost(10.0), memo.Cost(-7.0))
    52  	testSub(memo.Cost(10.0), memo.Cost(10.0), memo.Cost(0.0))
    53  }