1*bcb5dc79SHONG Yifan# Copyright 2018 The Bazel Authors. All rights reserved. 2*bcb5dc79SHONG Yifan# 3*bcb5dc79SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License"); 4*bcb5dc79SHONG Yifan# you may not use this file except in compliance with the License. 5*bcb5dc79SHONG Yifan# You may obtain a copy of the License at 6*bcb5dc79SHONG Yifan# 7*bcb5dc79SHONG Yifan# http://www.apache.org/licenses/LICENSE-2.0 8*bcb5dc79SHONG Yifan# 9*bcb5dc79SHONG Yifan# Unless required by applicable law or agreed to in writing, software 10*bcb5dc79SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS, 11*bcb5dc79SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*bcb5dc79SHONG Yifan# See the License for the specific language governing permissions and 13*bcb5dc79SHONG Yifan# limitations under the License. 14*bcb5dc79SHONG Yifan 15*bcb5dc79SHONG Yifan"""Unit tests for new_sets.bzl.""" 16*bcb5dc79SHONG Yifan 17*bcb5dc79SHONG Yifanload("//lib:new_sets.bzl", "sets") 18*bcb5dc79SHONG Yifanload("//lib:unittest.bzl", "asserts", "unittest") 19*bcb5dc79SHONG Yifan 20*bcb5dc79SHONG Yifandef _is_equal_test(ctx): 21*bcb5dc79SHONG Yifan """Unit tests for sets.is_equal.""" 22*bcb5dc79SHONG Yifan 23*bcb5dc79SHONG Yifan # Note that if this test fails, the results for the other `sets` tests will 24*bcb5dc79SHONG Yifan # be inconclusive because they use `asserts.new_set_equals`, which in turn 25*bcb5dc79SHONG Yifan # calls `sets.is_equal`. 26*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 27*bcb5dc79SHONG Yifan 28*bcb5dc79SHONG Yifan asserts.true(env, sets.is_equal(sets.make(), sets.make())) 29*bcb5dc79SHONG Yifan asserts.false(env, sets.is_equal(sets.make(), sets.make([1]))) 30*bcb5dc79SHONG Yifan asserts.false(env, sets.is_equal(sets.make([1]), sets.make())) 31*bcb5dc79SHONG Yifan asserts.true(env, sets.is_equal(sets.make([1]), sets.make([1]))) 32*bcb5dc79SHONG Yifan asserts.false(env, sets.is_equal(sets.make([1]), sets.make([1, 2]))) 33*bcb5dc79SHONG Yifan asserts.false(env, sets.is_equal(sets.make([1]), sets.make([2]))) 34*bcb5dc79SHONG Yifan asserts.false(env, sets.is_equal(sets.make([1]), sets.make([1, 2]))) 35*bcb5dc79SHONG Yifan 36*bcb5dc79SHONG Yifan # If passing a list, verify that duplicate elements are ignored. 37*bcb5dc79SHONG Yifan asserts.true(env, sets.is_equal(sets.make([1, 1]), sets.make([1]))) 38*bcb5dc79SHONG Yifan 39*bcb5dc79SHONG Yifan return unittest.end(env) 40*bcb5dc79SHONG Yifan 41*bcb5dc79SHONG Yifanis_equal_test = unittest.make(_is_equal_test) 42*bcb5dc79SHONG Yifan 43*bcb5dc79SHONG Yifandef _is_subset_test(ctx): 44*bcb5dc79SHONG Yifan """Unit tests for sets.is_subset.""" 45*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 46*bcb5dc79SHONG Yifan 47*bcb5dc79SHONG Yifan asserts.true(env, sets.is_subset(sets.make(), sets.make())) 48*bcb5dc79SHONG Yifan asserts.true(env, sets.is_subset(sets.make(), sets.make([1]))) 49*bcb5dc79SHONG Yifan asserts.false(env, sets.is_subset(sets.make([1]), sets.make())) 50*bcb5dc79SHONG Yifan asserts.true(env, sets.is_subset(sets.make([1]), sets.make([1]))) 51*bcb5dc79SHONG Yifan asserts.true(env, sets.is_subset(sets.make([1]), sets.make([1, 2]))) 52*bcb5dc79SHONG Yifan asserts.false(env, sets.is_subset(sets.make([1]), sets.make([2]))) 53*bcb5dc79SHONG Yifan 54*bcb5dc79SHONG Yifan # If passing a list, verify that duplicate elements are ignored. 55*bcb5dc79SHONG Yifan asserts.true(env, sets.is_subset(sets.make([1, 1]), sets.make([1, 2]))) 56*bcb5dc79SHONG Yifan 57*bcb5dc79SHONG Yifan return unittest.end(env) 58*bcb5dc79SHONG Yifan 59*bcb5dc79SHONG Yifanis_subset_test = unittest.make(_is_subset_test) 60*bcb5dc79SHONG Yifan 61*bcb5dc79SHONG Yifandef _disjoint_test(ctx): 62*bcb5dc79SHONG Yifan """Unit tests for sets.disjoint.""" 63*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 64*bcb5dc79SHONG Yifan 65*bcb5dc79SHONG Yifan asserts.true(env, sets.disjoint(sets.make(), sets.make())) 66*bcb5dc79SHONG Yifan asserts.true(env, sets.disjoint(sets.make(), sets.make([1]))) 67*bcb5dc79SHONG Yifan asserts.true(env, sets.disjoint(sets.make([1]), sets.make())) 68*bcb5dc79SHONG Yifan asserts.false(env, sets.disjoint(sets.make([1]), sets.make([1]))) 69*bcb5dc79SHONG Yifan asserts.false(env, sets.disjoint(sets.make([1]), sets.make([1, 2]))) 70*bcb5dc79SHONG Yifan asserts.true(env, sets.disjoint(sets.make([1]), sets.make([2]))) 71*bcb5dc79SHONG Yifan 72*bcb5dc79SHONG Yifan # If passing a list, verify that duplicate elements are ignored. 73*bcb5dc79SHONG Yifan asserts.false(env, sets.disjoint(sets.make([1, 1]), sets.make([1, 2]))) 74*bcb5dc79SHONG Yifan 75*bcb5dc79SHONG Yifan return unittest.end(env) 76*bcb5dc79SHONG Yifan 77*bcb5dc79SHONG Yifandisjoint_test = unittest.make(_disjoint_test) 78*bcb5dc79SHONG Yifan 79*bcb5dc79SHONG Yifandef _intersection_test(ctx): 80*bcb5dc79SHONG Yifan """Unit tests for sets.intersection.""" 81*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 82*bcb5dc79SHONG Yifan 83*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make(), sets.intersection(sets.make(), sets.make())) 84*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make(), sets.intersection(sets.make(), sets.make([1]))) 85*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make(), sets.intersection(sets.make([1]), sets.make())) 86*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1]), sets.intersection(sets.make([1]), sets.make([1]))) 87*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1]), sets.intersection(sets.make([1]), sets.make([1, 2]))) 88*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make(), sets.intersection(sets.make([1]), sets.make([2]))) 89*bcb5dc79SHONG Yifan 90*bcb5dc79SHONG Yifan # If passing a list, verify that duplicate elements are ignored. 91*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1]), sets.intersection(sets.make([1, 1]), sets.make([1, 2]))) 92*bcb5dc79SHONG Yifan 93*bcb5dc79SHONG Yifan return unittest.end(env) 94*bcb5dc79SHONG Yifan 95*bcb5dc79SHONG Yifanintersection_test = unittest.make(_intersection_test) 96*bcb5dc79SHONG Yifan 97*bcb5dc79SHONG Yifandef _union_test(ctx): 98*bcb5dc79SHONG Yifan """Unit tests for sets.union.""" 99*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 100*bcb5dc79SHONG Yifan 101*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make(), sets.union()) 102*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1]), sets.union(sets.make([1]))) 103*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make(), sets.union(sets.make(), sets.make())) 104*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1]), sets.union(sets.make(), sets.make([1]))) 105*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1]), sets.union(sets.make([1]), sets.make())) 106*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1]), sets.union(sets.make([1]), sets.make([1]))) 107*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1, 2]), sets.union(sets.make([1]), sets.make([1, 2]))) 108*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1, 2]), sets.union(sets.make([1]), sets.make([2]))) 109*bcb5dc79SHONG Yifan 110*bcb5dc79SHONG Yifan # If passing a list, verify that duplicate elements are ignored. 111*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1, 2]), sets.union(sets.make([1, 1]), sets.make([1, 2]))) 112*bcb5dc79SHONG Yifan 113*bcb5dc79SHONG Yifan return unittest.end(env) 114*bcb5dc79SHONG Yifan 115*bcb5dc79SHONG Yifanunion_test = unittest.make(_union_test) 116*bcb5dc79SHONG Yifan 117*bcb5dc79SHONG Yifandef _difference_test(ctx): 118*bcb5dc79SHONG Yifan """Unit tests for sets.difference.""" 119*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 120*bcb5dc79SHONG Yifan 121*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make(), sets.difference(sets.make(), sets.make())) 122*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make(), sets.difference(sets.make(), sets.make([1]))) 123*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1]), sets.difference(sets.make([1]), sets.make())) 124*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make(), sets.difference(sets.make([1]), sets.make([1]))) 125*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make(), sets.difference(sets.make([1]), sets.make([1, 2]))) 126*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1]), sets.difference(sets.make([1]), sets.make([2]))) 127*bcb5dc79SHONG Yifan 128*bcb5dc79SHONG Yifan # If passing a list, verify that duplicate elements are ignored. 129*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([2]), sets.difference(sets.make([1, 2]), sets.make([1, 1]))) 130*bcb5dc79SHONG Yifan 131*bcb5dc79SHONG Yifan return unittest.end(env) 132*bcb5dc79SHONG Yifan 133*bcb5dc79SHONG Yifandifference_test = unittest.make(_difference_test) 134*bcb5dc79SHONG Yifan 135*bcb5dc79SHONG Yifandef _to_list_test(ctx): 136*bcb5dc79SHONG Yifan """Unit tests for sets.to_list.""" 137*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 138*bcb5dc79SHONG Yifan 139*bcb5dc79SHONG Yifan asserts.equals(env, [], sets.to_list(sets.make())) 140*bcb5dc79SHONG Yifan asserts.equals(env, [1], sets.to_list(sets.make([1, 1, 1]))) 141*bcb5dc79SHONG Yifan asserts.equals(env, [1, 2, 3], sets.to_list(sets.make([1, 2, 3]))) 142*bcb5dc79SHONG Yifan 143*bcb5dc79SHONG Yifan return unittest.end(env) 144*bcb5dc79SHONG Yifan 145*bcb5dc79SHONG Yifanto_list_test = unittest.make(_to_list_test) 146*bcb5dc79SHONG Yifan 147*bcb5dc79SHONG Yifandef _make_test(ctx): 148*bcb5dc79SHONG Yifan """Unit tests for sets.make.""" 149*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 150*bcb5dc79SHONG Yifan 151*bcb5dc79SHONG Yifan asserts.equals(env, {}, sets.make()._values) 152*bcb5dc79SHONG Yifan asserts.equals(env, {x: None for x in [1, 2, 3]}, sets.make([1, 1, 2, 2, 3, 3])._values) 153*bcb5dc79SHONG Yifan 154*bcb5dc79SHONG Yifan return unittest.end(env) 155*bcb5dc79SHONG Yifan 156*bcb5dc79SHONG Yifanmake_test = unittest.make(_make_test) 157*bcb5dc79SHONG Yifan 158*bcb5dc79SHONG Yifandef _copy_test(ctx): 159*bcb5dc79SHONG Yifan """Unit tests for sets.copy.""" 160*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 161*bcb5dc79SHONG Yifan 162*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.copy(sets.make()), sets.make()) 163*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.copy(sets.make([1, 2, 3])), sets.make([1, 2, 3])) 164*bcb5dc79SHONG Yifan 165*bcb5dc79SHONG Yifan # Ensure mutating the copy does not mutate the original 166*bcb5dc79SHONG Yifan original = sets.make([1, 2, 3]) 167*bcb5dc79SHONG Yifan copy = sets.copy(original) 168*bcb5dc79SHONG Yifan copy._values[5] = None 169*bcb5dc79SHONG Yifan asserts.false(env, sets.is_equal(original, copy)) 170*bcb5dc79SHONG Yifan 171*bcb5dc79SHONG Yifan return unittest.end(env) 172*bcb5dc79SHONG Yifan 173*bcb5dc79SHONG Yifancopy_test = unittest.make(_copy_test) 174*bcb5dc79SHONG Yifan 175*bcb5dc79SHONG Yifandef _insert_test(ctx): 176*bcb5dc79SHONG Yifan """Unit tests for sets.insert.""" 177*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 178*bcb5dc79SHONG Yifan 179*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1, 2, 3]), sets.insert(sets.make([1, 2]), 3)) 180*bcb5dc79SHONG Yifan 181*bcb5dc79SHONG Yifan # Ensure mutating the inserted set does mutate the original set. 182*bcb5dc79SHONG Yifan original = sets.make([1, 2, 3]) 183*bcb5dc79SHONG Yifan after_insert = sets.insert(original, 4) 184*bcb5dc79SHONG Yifan asserts.new_set_equals( 185*bcb5dc79SHONG Yifan env, 186*bcb5dc79SHONG Yifan original, 187*bcb5dc79SHONG Yifan after_insert, 188*bcb5dc79SHONG Yifan msg = "Insert creates a new set which is an O(n) operation, insert should be O(1).", 189*bcb5dc79SHONG Yifan ) 190*bcb5dc79SHONG Yifan 191*bcb5dc79SHONG Yifan return unittest.end(env) 192*bcb5dc79SHONG Yifan 193*bcb5dc79SHONG Yifaninsert_test = unittest.make(_insert_test) 194*bcb5dc79SHONG Yifan 195*bcb5dc79SHONG Yifandef _contains_test(ctx): 196*bcb5dc79SHONG Yifan """Unit tests for sets.contains.""" 197*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 198*bcb5dc79SHONG Yifan 199*bcb5dc79SHONG Yifan asserts.false(env, sets.contains(sets.make(), 1)) 200*bcb5dc79SHONG Yifan asserts.true(env, sets.contains(sets.make([1]), 1)) 201*bcb5dc79SHONG Yifan asserts.true(env, sets.contains(sets.make([1, 2]), 1)) 202*bcb5dc79SHONG Yifan asserts.false(env, sets.contains(sets.make([2, 3]), 1)) 203*bcb5dc79SHONG Yifan 204*bcb5dc79SHONG Yifan return unittest.end(env) 205*bcb5dc79SHONG Yifan 206*bcb5dc79SHONG Yifancontains_test = unittest.make(_contains_test) 207*bcb5dc79SHONG Yifan 208*bcb5dc79SHONG Yifandef _length_test(ctx): 209*bcb5dc79SHONG Yifan """Unit test for sets.length.""" 210*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 211*bcb5dc79SHONG Yifan 212*bcb5dc79SHONG Yifan asserts.equals(env, 0, sets.length(sets.make())) 213*bcb5dc79SHONG Yifan asserts.equals(env, 1, sets.length(sets.make([1]))) 214*bcb5dc79SHONG Yifan asserts.equals(env, 2, sets.length(sets.make([1, 2]))) 215*bcb5dc79SHONG Yifan 216*bcb5dc79SHONG Yifan return unittest.end(env) 217*bcb5dc79SHONG Yifan 218*bcb5dc79SHONG Yifanlength_test = unittest.make(_length_test) 219*bcb5dc79SHONG Yifan 220*bcb5dc79SHONG Yifandef _remove_test(ctx): 221*bcb5dc79SHONG Yifan """Unit test for sets.remove.""" 222*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 223*bcb5dc79SHONG Yifan 224*bcb5dc79SHONG Yifan asserts.new_set_equals(env, sets.make([1, 2]), sets.remove(sets.make([1, 2, 3]), 3)) 225*bcb5dc79SHONG Yifan 226*bcb5dc79SHONG Yifan # Ensure mutating the inserted set does mutate the original set. 227*bcb5dc79SHONG Yifan original = sets.make([1, 2, 3]) 228*bcb5dc79SHONG Yifan after_removal = sets.remove(original, 3) 229*bcb5dc79SHONG Yifan asserts.new_set_equals(env, original, after_removal) 230*bcb5dc79SHONG Yifan 231*bcb5dc79SHONG Yifan return unittest.end(env) 232*bcb5dc79SHONG Yifan 233*bcb5dc79SHONG Yifanremove_test = unittest.make(_remove_test) 234*bcb5dc79SHONG Yifan 235*bcb5dc79SHONG Yifandef _repr_str_test(ctx): 236*bcb5dc79SHONG Yifan """Unit test for sets.repr and sets.str.""" 237*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 238*bcb5dc79SHONG Yifan 239*bcb5dc79SHONG Yifan asserts.equals(env, "[]", sets.repr(sets.make())) 240*bcb5dc79SHONG Yifan asserts.equals(env, "[1]", sets.repr(sets.make([1]))) 241*bcb5dc79SHONG Yifan asserts.equals(env, "[1, 2]", sets.repr(sets.make([1, 2]))) 242*bcb5dc79SHONG Yifan 243*bcb5dc79SHONG Yifan asserts.equals(env, "[]", sets.str(sets.make())) 244*bcb5dc79SHONG Yifan asserts.equals(env, "[1]", sets.str(sets.make([1]))) 245*bcb5dc79SHONG Yifan asserts.equals(env, "[1, 2]", sets.str(sets.make([1, 2]))) 246*bcb5dc79SHONG Yifan 247*bcb5dc79SHONG Yifan return unittest.end(env) 248*bcb5dc79SHONG Yifan 249*bcb5dc79SHONG Yifanrepr_str_test = unittest.make(_repr_str_test) 250*bcb5dc79SHONG Yifan 251*bcb5dc79SHONG Yifandef new_sets_test_suite(): 252*bcb5dc79SHONG Yifan """Creates the test targets and test suite for new_sets.bzl tests.""" 253*bcb5dc79SHONG Yifan unittest.suite( 254*bcb5dc79SHONG Yifan "new_sets_tests", 255*bcb5dc79SHONG Yifan disjoint_test, 256*bcb5dc79SHONG Yifan intersection_test, 257*bcb5dc79SHONG Yifan is_equal_test, 258*bcb5dc79SHONG Yifan is_subset_test, 259*bcb5dc79SHONG Yifan difference_test, 260*bcb5dc79SHONG Yifan union_test, 261*bcb5dc79SHONG Yifan to_list_test, 262*bcb5dc79SHONG Yifan make_test, 263*bcb5dc79SHONG Yifan copy_test, 264*bcb5dc79SHONG Yifan insert_test, 265*bcb5dc79SHONG Yifan contains_test, 266*bcb5dc79SHONG Yifan length_test, 267*bcb5dc79SHONG Yifan remove_test, 268*bcb5dc79SHONG Yifan repr_str_test, 269*bcb5dc79SHONG Yifan ) 270