| 1 | // Mgmt |
| 2 | // Copyright (C) 2013-2024+ James Shubin and the project contributors |
| 3 | // Written by James Shubin <[email protected]> and the project contributors |
| 4 | // |
| 5 | // This program is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | // |
| 18 | // Additional permission under GNU GPL version 3 section 7 |
| 19 | // |
| 20 | // If you modify this program, or any covered work, by linking or combining it |
| 21 | // with embedded mcl code and modules (and that the embedded mcl code and |
| 22 | // modules which link with this program, contain a copy of their source code in |
| 23 | // the authoritative form) containing parts covered by the terms of any other |
| 24 | // license, the licensors of this program grant you additional permission to |
| 25 | // convey the resulting work. Furthermore, the licensors of this program grant |
| 26 | // the original author, James Shubin, additional permission to update this |
| 27 | // additional permission if he deems it necessary to achieve the goals of this |
| 28 | // additional permission. |
| 29 | |
| 30 | //go:build !root |
| 31 | |
| 32 | package disjointscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/ |
| 33 | |
| 34 | import ( |
| 35 | "sortscip-go gomod github.com/golang/go/src go1.21 sort/" |
| 36 | "testingscip-go gomod github.com/golang/go/src go1.21 testing/" |
| 37 | ) |
| 38 | |
| 39 | func TestUnionFind0scip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/TestUnionFind0().(t *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Tscip-go gomod github.com/golang/go/src go1.21 testing/T#) { |
| 40 | s1 := NewElemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/NewElem().[bool]() |
| 41 | s2 := NewElemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/NewElem().[bool]() |
| 42 | s3 := NewElemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/NewElem().[bool]() |
| 43 | |
| 44 | s1.Unionscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Union().(s2) |
| 45 | |
| 46 | f1 := s1.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().() |
| 47 | f2 := s2.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().() |
| 48 | f3 := s3.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().() |
| 49 | |
| 50 | if f1 != f2 || !IsConnectedscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/IsConnected().(f1, f2) { |
| 51 | t.Errorfscip-go gomod github.com/golang/go/src go1.21 testing/common#Errorf().("f1 and f2 are not in the same set") |
| 52 | } |
| 53 | if f2 == f3 || IsConnectedscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/IsConnected().(f2, f3) { |
| 54 | t.Errorfscip-go gomod github.com/golang/go/src go1.21 testing/common#Errorf().("f1 and f2 should not be in the same set") |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestMerge0scip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/TestMerge0().(t *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Tscip-go gomod github.com/golang/go/src go1.21 testing/T#) { |
| 59 | s1 := NewElemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/NewElem().[[]string]() |
| 60 | s1.Datascip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Data. = []string{"a"} |
| 61 | s2 := NewElemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/NewElem().[[]string]() |
| 62 | s2.Datascip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Data. = []string{"b"} |
| 63 | s3 := NewElemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/NewElem().[[]string]() |
| 64 | s3.Datascip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Data. = []string{"c"} |
| 65 | |
| 66 | //s1.Union(s2) |
| 67 | //s2.Union(s3) |
| 68 | merge := func(a, b []string) ([]string, error) { |
| 69 | t.Logfscip-go gomod github.com/golang/go/src go1.21 testing/common#Logf().("merge: `%s` and `%s`", a, b) |
| 70 | c := []string{} |
| 71 | c = append(c, a...) |
| 72 | c = append(c, b...) |
| 73 | return c, nil |
| 74 | } |
| 75 | if err := UnsafeMergescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/UnsafeMerge().(s1, s2, merge); err != nil { |
| 76 | t.Errorfscip-go gomod github.com/golang/go/src go1.21 testing/common#Errorf().("merge error: %v", err) |
| 77 | return |
| 78 | } |
| 79 | |
| 80 | // If this is UnsafeMerge, we should correctly fail! |
| 81 | if err := Mergescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Merge().(s2, s3, merge); err != nil { |
| 82 | t.Errorfscip-go gomod github.com/golang/go/src go1.21 testing/common#Errorf().("merge error: %v", err) |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | x := s3.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().().Datascip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Data. |
| 87 | sortscip-go gomod github.com/golang/go/src go1.21 sort/.Stringsscip-go gomod github.com/golang/go/src go1.21 sort/Strings().(x) |
| 88 | |
| 89 | // TODO: use compare in golang 1.21 |
| 90 | //if !slices.Compare(x, []string{"a", "b", "c"}) { |
| 91 | // t.Errorf("wrong data") |
| 92 | //} |
| 93 | if x[0] != "a" || x[1] != "b" || x[2] != "c" { |
| 94 | t.Errorfscip-go gomod github.com/golang/go/src go1.21 testing/common#Errorf().("wrong data, got: %v", x) |
| 95 | } |
| 96 | } |