101e04c3fSmrg/* 201e04c3fSmrg * Copyright © 2018 Intel Corporation 301e04c3fSmrg * 401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 501e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 601e04c3fSmrg * to deal in the Software without restriction, including without limitation 701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 901e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1001e04c3fSmrg * 1101e04c3fSmrg * The above copyright notice and this permission notice (including the next 1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the 1301e04c3fSmrg * Software. 1401e04c3fSmrg * 1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2101e04c3fSmrg * IN THE SOFTWARE. 2201e04c3fSmrg */ 2301e04c3fSmrg 2401e04c3fSmrg#include <gtest/gtest.h> 2501e04c3fSmrg#include "util/hash_table.h" 2601e04c3fSmrg#include "util/set.h" 2701e04c3fSmrg 2801e04c3fSmrgTEST(set, basic) 2901e04c3fSmrg{ 3001e04c3fSmrg struct set *s = _mesa_set_create(NULL, _mesa_hash_pointer, 3101e04c3fSmrg _mesa_key_pointer_equal); 3201e04c3fSmrg struct set_entry *entry; 3301e04c3fSmrg 3401e04c3fSmrg const void *a = (const void *)10; 3501e04c3fSmrg const void *b = (const void *)20; 3601e04c3fSmrg 3701e04c3fSmrg _mesa_set_add(s, a); 3801e04c3fSmrg _mesa_set_add(s, b); 3901e04c3fSmrg EXPECT_EQ(s->entries, 2); 4001e04c3fSmrg 4101e04c3fSmrg _mesa_set_add(s, a); 4201e04c3fSmrg EXPECT_EQ(s->entries, 2); 4301e04c3fSmrg 4401e04c3fSmrg entry = _mesa_set_search(s, a); 4501e04c3fSmrg EXPECT_TRUE(entry); 4601e04c3fSmrg EXPECT_EQ(entry->key, a); 4701e04c3fSmrg 4801e04c3fSmrg _mesa_set_remove(s, entry); 4901e04c3fSmrg EXPECT_EQ(s->entries, 1); 5001e04c3fSmrg 5101e04c3fSmrg entry = _mesa_set_search(s, a); 5201e04c3fSmrg EXPECT_FALSE(entry); 5301e04c3fSmrg 547ec681f3Smrg _mesa_set_clear(s, NULL); 557ec681f3Smrg EXPECT_EQ(s->entries, 0); 567ec681f3Smrg EXPECT_EQ(s->deleted_entries, 0); 577ec681f3Smrg set_foreach(s, he) { 587ec681f3Smrg GTEST_FAIL(); 597ec681f3Smrg } 607ec681f3Smrg 617ec681f3Smrg _mesa_set_add(s, a); 627ec681f3Smrg _mesa_set_add(s, b); 637ec681f3Smrg EXPECT_EQ(s->entries, 2); 647ec681f3Smrg unsigned count = s->entries; 657ec681f3Smrg set_foreach_remove(s, he) { 667ec681f3Smrg EXPECT_TRUE(he->key == a || he->key == b); 677ec681f3Smrg EXPECT_EQ(s->entries, count--); 687ec681f3Smrg EXPECT_EQ(s->deleted_entries, 0); 697ec681f3Smrg } 707ec681f3Smrg EXPECT_EQ(s->entries, 0); 717ec681f3Smrg set_foreach(s, he) { 727ec681f3Smrg GTEST_FAIL(); 737ec681f3Smrg } 747ec681f3Smrg 7501e04c3fSmrg _mesa_set_destroy(s, NULL); 7601e04c3fSmrg} 7701e04c3fSmrg 7801e04c3fSmrgTEST(set, clone) 7901e04c3fSmrg{ 8001e04c3fSmrg struct set *s = _mesa_set_create(NULL, _mesa_hash_pointer, 8101e04c3fSmrg _mesa_key_pointer_equal); 8201e04c3fSmrg struct set_entry *entry; 8301e04c3fSmrg 8401e04c3fSmrg const void *a = (const void *)10; 8501e04c3fSmrg const void *b = (const void *)20; 8601e04c3fSmrg const void *c = (const void *)30; 8701e04c3fSmrg 8801e04c3fSmrg _mesa_set_add(s, a); 8901e04c3fSmrg _mesa_set_add(s, b); 9001e04c3fSmrg _mesa_set_add(s, c); 9101e04c3fSmrg 9201e04c3fSmrg entry = _mesa_set_search(s, c); 9301e04c3fSmrg EXPECT_TRUE(entry); 9401e04c3fSmrg EXPECT_EQ(entry->key, c); 9501e04c3fSmrg 9601e04c3fSmrg _mesa_set_remove(s, entry); 9701e04c3fSmrg EXPECT_EQ(s->entries, 2); 9801e04c3fSmrg 9901e04c3fSmrg struct set *clone = _mesa_set_clone(s, NULL); 10001e04c3fSmrg EXPECT_EQ(clone->entries, 2); 10101e04c3fSmrg 10201e04c3fSmrg EXPECT_TRUE(_mesa_set_search(clone, a)); 10301e04c3fSmrg EXPECT_TRUE(_mesa_set_search(clone, b)); 10401e04c3fSmrg EXPECT_FALSE(_mesa_set_search(clone, c)); 10501e04c3fSmrg 10601e04c3fSmrg _mesa_set_destroy(s, NULL); 10701e04c3fSmrg _mesa_set_destroy(clone, NULL); 10801e04c3fSmrg} 10901e04c3fSmrg 11001e04c3fSmrgTEST(set, remove_key) 11101e04c3fSmrg{ 11201e04c3fSmrg struct set *s = _mesa_set_create(NULL, _mesa_hash_pointer, 11301e04c3fSmrg _mesa_key_pointer_equal); 11401e04c3fSmrg 11501e04c3fSmrg const void *a = (const void *)10; 11601e04c3fSmrg const void *b = (const void *)20; 11701e04c3fSmrg const void *c = (const void *)30; 11801e04c3fSmrg 11901e04c3fSmrg _mesa_set_add(s, a); 12001e04c3fSmrg _mesa_set_add(s, b); 12101e04c3fSmrg EXPECT_EQ(s->entries, 2); 12201e04c3fSmrg 12301e04c3fSmrg /* Remove existing key. */ 12401e04c3fSmrg _mesa_set_remove_key(s, a); 12501e04c3fSmrg EXPECT_EQ(s->entries, 1); 12601e04c3fSmrg EXPECT_FALSE(_mesa_set_search(s, a)); 12701e04c3fSmrg EXPECT_TRUE(_mesa_set_search(s, b)); 12801e04c3fSmrg 12901e04c3fSmrg /* Remove non-existing key. */ 13001e04c3fSmrg _mesa_set_remove_key(s, c); 13101e04c3fSmrg EXPECT_EQ(s->entries, 1); 13201e04c3fSmrg EXPECT_FALSE(_mesa_set_search(s, a)); 13301e04c3fSmrg EXPECT_TRUE(_mesa_set_search(s, b)); 13401e04c3fSmrg 13501e04c3fSmrg _mesa_set_destroy(s, NULL); 13601e04c3fSmrg} 1377ec681f3Smrg 1387ec681f3Smrgstatic uint32_t hash_int(const void *p) 1397ec681f3Smrg{ 1407ec681f3Smrg int i = *(const int *)p; 1417ec681f3Smrg return i; 1427ec681f3Smrg} 1437ec681f3Smrg 1447ec681f3Smrgstatic bool cmp_int(const void *p1, const void *p2) 1457ec681f3Smrg{ 1467ec681f3Smrg int i1 = *(const int *)p1, i2 = *(const int *)p2; 1477ec681f3Smrg return i1 == i2; 1487ec681f3Smrg} 1497ec681f3Smrg 1507ec681f3SmrgTEST(set, search_or_add) 1517ec681f3Smrg{ 1527ec681f3Smrg struct set *s = _mesa_set_create(NULL, hash_int, cmp_int); 1537ec681f3Smrg 1547ec681f3Smrg int a = 10, b = 20, c = 20, d = 30; 1557ec681f3Smrg 1567ec681f3Smrg _mesa_set_add(s, &a); 1577ec681f3Smrg _mesa_set_add(s, &b); 1587ec681f3Smrg EXPECT_EQ(s->entries, 2); 1597ec681f3Smrg 1607ec681f3Smrg bool found = false; 1617ec681f3Smrg struct set_entry *entry = _mesa_set_search_or_add(s, &c, &found); 1627ec681f3Smrg EXPECT_EQ(entry->key, (void *)&b); 1637ec681f3Smrg EXPECT_EQ(found, true); 1647ec681f3Smrg EXPECT_EQ(s->entries, 2); 1657ec681f3Smrg 1667ec681f3Smrg found = false; 1677ec681f3Smrg struct set_entry *entry3 = _mesa_set_search_or_add(s, &d, &found); 1687ec681f3Smrg EXPECT_EQ(entry3->key, &d); 1697ec681f3Smrg EXPECT_EQ(found, false); 1707ec681f3Smrg EXPECT_EQ(s->entries, 3); 1717ec681f3Smrg 1727ec681f3Smrg _mesa_set_destroy(s, NULL); 1737ec681f3Smrg} 174