|
| 1 | +// Copyright 2023 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 tests |
| 12 | + |
| 13 | +import ( |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 17 | +) |
| 18 | + |
| 19 | +// TestUnsortedMatricesDiff is a unit test for the |
| 20 | +// unsortedMatricesDiffWithFloatComp() and unsortedMatricesDiff() utility |
| 21 | +// functions. |
| 22 | +func TestUnsortedMatricesDiff(t *testing.T) { |
| 23 | + defer leaktest.AfterTest(t)() |
| 24 | + tcs := []struct { |
| 25 | + name string |
| 26 | + colTypes []string |
| 27 | + t1, t2 [][]string |
| 28 | + exactMatch bool |
| 29 | + approxMatch bool |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "float exact match", |
| 33 | + colTypes: []string{"FLOAT8"}, |
| 34 | + t1: [][]string{{"1.2345678901234567"}}, |
| 35 | + t2: [][]string{{"1.2345678901234567"}}, |
| 36 | + exactMatch: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "float approx match", |
| 40 | + colTypes: []string{"FLOAT8"}, |
| 41 | + t1: [][]string{{"1.2345678901234563"}}, |
| 42 | + t2: [][]string{{"1.2345678901234564"}}, |
| 43 | + exactMatch: false, |
| 44 | + approxMatch: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "float no match", |
| 48 | + colTypes: []string{"FLOAT8"}, |
| 49 | + t1: [][]string{{"1.234567890123"}}, |
| 50 | + t2: [][]string{{"1.234567890124"}}, |
| 51 | + exactMatch: false, |
| 52 | + approxMatch: false, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "multi float approx match", |
| 56 | + colTypes: []string{"FLOAT8", "FLOAT8"}, |
| 57 | + t1: [][]string{{"1.2345678901234567", "1.2345678901234567"}}, |
| 58 | + t2: [][]string{{"1.2345678901234567", "1.2345678901234568"}}, |
| 59 | + exactMatch: false, |
| 60 | + approxMatch: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "string no match", |
| 64 | + colTypes: []string{"STRING"}, |
| 65 | + t1: [][]string{{"hello"}}, |
| 66 | + t2: [][]string{{"world"}}, |
| 67 | + exactMatch: false, |
| 68 | + approxMatch: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "mixed types match", |
| 72 | + colTypes: []string{"STRING", "FLOAT8"}, |
| 73 | + t1: [][]string{{"hello", "1.2345678901234567"}}, |
| 74 | + t2: [][]string{{"hello", "1.2345678901234567"}}, |
| 75 | + exactMatch: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "mixed types float approx match", |
| 79 | + colTypes: []string{"STRING", "FLOAT8"}, |
| 80 | + t1: [][]string{{"hello", "1.23456789012345678"}}, |
| 81 | + t2: [][]string{{"hello", "1.23456789012345679"}}, |
| 82 | + exactMatch: false, |
| 83 | + approxMatch: true, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "mixed types no match", |
| 87 | + colTypes: []string{"STRING", "FLOAT8"}, |
| 88 | + t1: [][]string{{"hello", "1.2345678901234567"}}, |
| 89 | + t2: [][]string{{"world", "1.2345678901234567"}}, |
| 90 | + exactMatch: false, |
| 91 | + approxMatch: false, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "different col count", |
| 95 | + colTypes: []string{"STRING"}, |
| 96 | + t1: [][]string{{"hello", "1.2345678901234567"}}, |
| 97 | + t2: [][]string{{"world", "1.2345678901234567"}}, |
| 98 | + exactMatch: false, |
| 99 | + approxMatch: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "different row count", |
| 103 | + colTypes: []string{"STRING", "FLOAT8"}, |
| 104 | + t1: [][]string{{"hello", "1.2345678901234567"}, {"aloha", "2.345"}}, |
| 105 | + t2: [][]string{{"world", "1.2345678901234567"}}, |
| 106 | + exactMatch: false, |
| 107 | + approxMatch: false, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "multi row unsorted", |
| 111 | + colTypes: []string{"STRING", "FLOAT8"}, |
| 112 | + t1: [][]string{{"hello", "1.2345678901234567"}, {"world", "1.2345678901234560"}}, |
| 113 | + t2: [][]string{{"world", "1.2345678901234560"}, {"hello", "1.2345678901234567"}}, |
| 114 | + exactMatch: true, |
| 115 | + }, |
| 116 | + } |
| 117 | + for _, tc := range tcs { |
| 118 | + t.Run(tc.name, func(t *testing.T) { |
| 119 | + match := unsortedMatricesDiff(tc.t1, tc.t2) |
| 120 | + if tc.exactMatch && match != "" { |
| 121 | + t.Fatalf("unsortedMatricesDiff: expected exact match, got diff: %s", match) |
| 122 | + } else if !tc.exactMatch && match == "" { |
| 123 | + t.Fatalf("unsortedMatricesDiff: expected no exact match, got no diff") |
| 124 | + } |
| 125 | + |
| 126 | + var err error |
| 127 | + match, err = unsortedMatricesDiffWithFloatComp(tc.t1, tc.t2, tc.colTypes) |
| 128 | + if err != nil { |
| 129 | + t.Fatal(err) |
| 130 | + } |
| 131 | + if tc.exactMatch && match != "" { |
| 132 | + t.Fatalf("unsortedMatricesDiffWithFloatComp: expected exact match, got diff: %s", match) |
| 133 | + } else if !tc.exactMatch && tc.approxMatch && match != "" { |
| 134 | + t.Fatalf("unsortedMatricesDiffWithFloatComp: expected approx match, got diff: %s", match) |
| 135 | + } else if !tc.exactMatch && !tc.approxMatch && match == "" { |
| 136 | + t.Fatalf("unsortedMatricesDiffWithFloatComp: expected no approx match, got no diff") |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
0 commit comments