github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/props/cardinality_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 props_test
    12  
    13  import (
    14  	"math"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/sql/opt/props"
    18  )
    19  
    20  func TestCardinality(t *testing.T) {
    21  	test := func(card, expected props.Cardinality) {
    22  		t.Helper()
    23  		if card != expected {
    24  			t.Errorf("expected: %s, actual: %s", expected, card)
    25  		}
    26  	}
    27  
    28  	c := func(min, max uint32) props.Cardinality {
    29  		return props.Cardinality{Min: min, Max: max}
    30  	}
    31  	inf := uint32(math.MaxUint32)
    32  
    33  	// AsLowAs variations.
    34  	test(c(0, 10).AsLowAs(0), c(0, 10))
    35  	test(c(1, 10).AsLowAs(0), c(0, 10))
    36  	test(c(5, 10).AsLowAs(1), c(1, 10))
    37  	test(c(1, 10).AsLowAs(5), c(1, 10))
    38  	test(c(1, 10).AsLowAs(20), c(1, 10))
    39  	test(props.AnyCardinality.AsLowAs(1), c(0, inf))
    40  
    41  	// Limit variations.
    42  	test(c(0, 10).Limit(5), c(0, 5))
    43  	test(c(1, 10).Limit(10), c(1, 10))
    44  	test(c(5, 10).Limit(1), c(1, 1))
    45  	test(props.AnyCardinality.Limit(1), c(0, 1))
    46  
    47  	// AtLeast variations.
    48  	test(c(0, 10).AtLeast(c(1, 1)), c(1, 10))
    49  	test(c(1, 10).AtLeast(c(5, 15)), c(5, 15))
    50  	test(c(5, 10).AtLeast(c(1, 2)), c(5, 10))
    51  	test(c(5, 10).AtLeast(c(1, 8)), c(5, 10))
    52  	test(c(5, 10).AtLeast(c(7, 8)), c(7, 10))
    53  	test(c(5, 10).AtLeast(c(1, 15)), c(5, 15))
    54  	test(c(5, 10).AtLeast(c(7, 15)), c(7, 15))
    55  	test(props.AnyCardinality.AtLeast(c(1, 10)), c(1, inf))
    56  	test(props.AnyCardinality.AtLeast(c(inf, inf)), c(inf, inf))
    57  
    58  	// Add variations.
    59  	test(c(0, 10).Add(c(5, 5)), c(5, 15))
    60  	test(c(0, 10).Add(c(20, 30)), c(20, 40))
    61  	test(c(1, 10).Add(props.AnyCardinality), c(1, inf))
    62  	test(c(inf, inf).Add(props.AnyCardinality), c(inf, inf))
    63  
    64  	// Product variations.
    65  	test(c(0, 10).Product(c(5, 5)), c(0, 50))
    66  	test(c(1, 10).Product(c(2, 2)), c(2, 20))
    67  	test(c(1, 10).Product(props.AnyCardinality), c(0, inf))
    68  	test(c(inf, inf).Product(props.OneCardinality), c(inf, inf))
    69  	test(c(inf, inf).Product(c(inf, inf)), c(inf, inf))
    70  
    71  	// Skip variations.
    72  	test(c(0, 0).Skip(1), c(0, 0))
    73  	test(c(0, 10).Skip(5), c(0, 5))
    74  	test(c(5, 10).Skip(5), c(0, 5))
    75  	test(props.AnyCardinality.Skip(5), c(0, inf))
    76  	test(c(inf, inf).Skip(5), c(inf-5, inf))
    77  }