/* * create two sets with n random elements * the sets should have 50% of the elements in common */ void createRandomSetN(int n, Set* a, Set* b) { int x; int last_size = 0; createEmptySet(a); while (a->len < n) { if (a->len != last_size) { last_size = a->len; if (last_size % 1000 == 0) { printf("%d..", last_size); fflush(stdout); } } x = 2 * (5 * n - (rand() % (10 * n))); if (isMemberSet(a, x)) { continue; } // try another number /* a will have only even elements */ insertSet(a, x); if ((rand() % 2) == 0) { insertSet(b, x); } else { insertSet(b, x + 1); // an odd value, can't be in a } } assert(a->len == b->len); }
//! indicate the two formal params and/or globals may alias each //! other at a particular call therefore should be in same map void SymAliasSets::mergeSyms(SymHandle sym1, SymHandle sym2) { // look at what both symbols are mapped to int symSet1 = getAliasSetId(sym1); int symSet2 = getAliasSetId(sym2); // if neither of them are in a set yet then make a set, put them // in it, and insert set if (symSet1==SET_ID_NONE && symSet2==SET_ID_NONE) { OA_ptr<SymSet> newSet; newSet = new SymSet; newSet->insert(sym1); newSet->insert(sym2); insertSet(newSet); } // if they are in different sets // then create a new set with them both in it and insert the new set // also remove old sets if (symSet1!=symSet2) { // just insert sym1 into symSet2 if sym1 is not in a set already if (symSet1==SET_ID_NONE) { mIdToSymSetMap[symSet2]->insert(sym1); mSymToIdMap[sym1] = symSet2; // same goes for symSet2 } else if (symSet2==SET_ID_NONE) { mIdToSymSetMap[symSet1]->insert(sym2); mSymToIdMap[sym2] = symSet1; // otherwise merge the two sets } else { // union them into the first set mIdToSymSetMap[symSet1]->setUnion(*mIdToSymSetMap[symSet2]); // map everything in second set to the first set OA_ptr<SymSetIterator> symIter; symIter = new SymSetIterator( mIdToSymSetMap[symSet2] ); for ( ; symIter->isValid(); (*symIter)++ ) { mSymToIdMap[symIter->current()] = symSet1; } // replace second set with an empty set OA_ptr<SymSet> emptySet; emptySet = new SymSet; mIdToSymSetMap[symSet2] = emptySet; } } }
void rec(char* s, int n, int c) { if (c >= n - 1 && isBalanced(s,n) && !isPresent(s,n)) { insertSet(s, n); for (int i = 0; i < n; i++) { cout << s[i]; } cout << endl; return; } for (int i = c; i < n; i++) { swp(s[i], s[c]); rec(s, n, c + 1); swp(s[i], s[c]); } }
void equalityTests(void) { int i; for (i = 0; i < number_of_tests; i += 1) { Set s; Set t; createEmptySet(&s); randomSet(&s); createCopySet(&t, &s); assert(isEqualToSet(&t, &s)); assert(isEqualToSet(&s, &t)); insertSet(&t, maximum_set_size); assert(! isEqualToSet(&s, &t)); assert(! isEqualToSet(&t, &s)); randomSet(&t); assert(! isEqualToSet(&t, &s)); destroySet(&s); destroySet(&t); } // This test could fail with small probability printf("The equality tests have been passed\n"); }
void relationalTests() { Set s; Set t; int i; createEmptySet(&s); createEmptySet(&t); for (i = 0; i < number_of_tests; i += 1) { randomSet(&s); assignSet(&t, &s); assert(isSubsetOf(&s, &t)); assert(isSubsetOf(&t, &s)); assert(isEqualToSet(&s, &t)); assert(isEqualToSet(&t, &s)); insertSet(&s, rand() % maximum_set_size + maximum_set_size); assert(isSubsetOf(&t, &s)); assert(! isSubsetOf(&s, &t)); } printf("The relational tests have been passed\n"); destroySet(&s); destroySet(&t); }
void algebraicTests(void) { Set empty; Set universal; int i; Set s; Set t; Set u; Set v; Set w; createEmptySet(&empty); createEmptySet(&universal); for (i = 0; i < maximum_set_size; i += 1) { insertSet(&universal, i); } createEmptySet(&s); createEmptySet(&t); createEmptySet(&u); createEmptySet(&v); createEmptySet(&w); for (i = 0; i < number_of_tests; i += 1) { randomSet(&u); randomSet(&v); randomSet(&w); /* u + v == v + u */ assignSet(&s, &u); unionInSet(&s, &v); assignSet(&t, &v); unionInSet(&t, &u); assert(isEqualToSet(&s, &t)); /* u + (v + w) == (u + v) + w */ assignSet(&t, &v); unionInSet(&t, &w); assignSet(&s, &u); unionInSet(&s, &t); assignSet(&t, &u); unionInSet(&t, &v); unionInSet(&t, &w); assert(isEqualToSet(&s, &t)); /* u * v == v * u */ assignSet(&s, &u); intersectFromSet(&s, &v); assignSet(&t, &v); intersectFromSet(&t, &u); assert(isEqualToSet(&s, &t)); /* u * (v * w) == (u * v) * w */ assignSet(&t, &v); intersectFromSet(&t, &w); assignSet(&s, &u); intersectFromSet(&s, &t); assignSet(&t, &u); intersectFromSet(&t, &v); intersectFromSet(&t, &w); assert(isEqualToSet(&s, &t)); /* u - v == u - (u * v) */ assignSet(&s, &u); intersectFromSet(&s, &v); assignSet(&t, &u); subtractFromSet(&t, &s); assignSet(&s, &u); subtractFromSet(&s, &v); assert(isEqualToSet(&s, &t)); /* additional tests, not implemented assert(w * (u - v) == w * u - w * v); assert(u * (v + w) == (u * v) + (u * w)); assert(universal - (u * v) == (universal - u) + (universal - v)); assert(universal - (u + v) == (universal - u) * (universal - v)); */ } printf("The algebraic tests have been passed\n"); destroySet(&empty); destroySet(&universal); destroySet(&s); destroySet(&t); destroySet(&u); destroySet(&v); destroySet(&w); }
void specialCaseTests(void) { Set empty; Set universal; int i; Set s; Set r; createEmptySet(&empty); createEmptySet(&universal); createEmptySet(&r); for (i = 0; i < maximum_set_size; i += 1) { insertSet(&universal, i); } checkCase(&subtractFromSet, &universal, &universal, &empty); checkCase(&unionInSet, &universal, &universal, &universal); checkCase(&intersectFromSet, &universal, &universal, &universal); checkCase(&intersectFromSet, &universal, &empty, &empty); checkCase(&intersectFromSet, &empty, &universal, &empty); checkCase(&unionInSet, &universal, &empty, &universal); checkCase(&unionInSet, &empty, &universal, &universal); checkCase(&unionInSet, &empty, &empty, &empty); checkCase(&subtractFromSet, &empty, &empty, &empty); checkCase(&intersectFromSet, &empty, &empty, &empty); createEmptySet(&s); assert(isEmptySet(&s)); for (i = 0; i < 10; i += 1) { insertSet(&s, i); } assert(s.len == 10); for (i = 0; i < 10; i += 1) { assert(isMemberSet(&s, i)); } for (i = 0; i < 10; i += 1) { removeSet(&s, i); removeSet(&s, i); assert(s.len == 9 - i); } assert(isEmptySet(&s)); for (i = 0; i < number_of_tests; i += 1) { randomSet(&s); assert(isSubsetOf(&empty, &s)); assert(!isSubsetOf(&s, &empty)); assert(isSubsetOf(&s, &universal)); assert(!isSubsetOf(&universal, &s)); checkCase(&intersectFromSet, &empty, &s, &empty); checkCase(&intersectFromSet, &s, &empty, &empty); checkCase(&intersectFromSet, &universal, &s, &s); checkCase(&intersectFromSet, &s, &universal, &s); checkCase(&unionInSet, &universal, &s, &universal); checkCase(&unionInSet, &s, &universal, &universal); checkCase(&subtractFromSet, &s, &empty, &s); assignSet(&r, &universal); subtractFromSet(&r, &s); // r = u - s; checkCase(&subtractFromSet, &universal, &r, &s); // (u - (u - s) == s) checkCase(&unionInSet, &s, &r, &universal); // s + (u - s) == u checkCase(&unionInSet, &r, &s, &universal); // (u - s) + s == u } printf("The special case tests have been passed\n"); destroySet(&empty); destroySet(&universal); destroySet(&s); destroySet(&r); }
TEST(Test07, AlgebraicTests) { { Set empty; Set universal; Set r; Set s; Set t; Set u; Set v; Set w; int i; createEmptySet(&empty); createEmptySet(&universal); for (i = 0; i < MAX_SET_SIZE; i++) { insertSet(&universal, i); } createEmptySet(&r); createEmptySet(&s); createEmptySet(&t); createEmptySet(&u); createEmptySet(&v); createEmptySet(&w); ASSERT_FALSE(isEqualToSet(&empty, &universal)); for (i = 0; i < NUM_TESTS; i++) { randomSet(&u); randomSet(&v); randomSet(&w); /* w * (u - v) == w * u - w * v */ assignSet(&s, &u); subtractFromSet(&s, &v); assignSet(&r, &w); intersectFromSet(&r, &s); assignSet(&t, &w); intersectFromSet(&t, &v); assignSet(&s, &w); intersectFromSet(&s, &u); subtractFromSet(&s, &t); ASSERT_TRUE(isEqualToSet(&r, &s)); /* u * (v + w) == (u * v) + (u * w) */ assignSet(&s, &v); unionInSet(&s, &w); assignSet(&r, &u); intersectFromSet(&r, &s); assignSet(&t, &u); intersectFromSet(&t, &w); assignSet(&s, &u); intersectFromSet(&s, &v); unionInSet(&s, &t); ASSERT_TRUE(isEqualToSet(&r, &s)); /* universal - (u * v) == (universal - u) + (universal - v) */ assignSet(&s, &u); intersectFromSet(&s, &v); assignSet(&r, &universal); subtractFromSet(&r, &s); assignSet(&t, &universal); subtractFromSet(&t, &v); assignSet(&s, &universal); subtractFromSet(&s, &u); unionInSet(&s, &t); ASSERT_TRUE(isEqualToSet(&r, &s)); /* universal - (u + v) == (universal - u) * (universal - v) */ assignSet(&s, &u); unionInSet(&s, &v); assignSet(&r, &universal); subtractFromSet(&r, &s); assignSet(&t, &universal); subtractFromSet(&t, &v); assignSet(&s, &universal); subtractFromSet(&s, &u); intersectFromSet(&s, &t); ASSERT_TRUE(isEqualToSet(&r, &s)); } destroySet(&empty); destroySet(&universal); destroySet(&r); destroySet(&s); destroySet(&t); destroySet(&u); destroySet(&v); destroySet(&w); } }
TEST(Test05, SpecialCaseTests) { { Set empty; Set universal; Set s; Set r; int i; createEmptySet(&empty); createEmptySet(&universal); createEmptySet(&r); for (i = 0; i < MAX_SET_SIZE; i++) { insertSet(&universal, i); } ASSERT_TRUE(checkCaseNew(&subtractFromSet, &universal, &universal, &empty)); ASSERT_TRUE(checkCaseNew(&unionInSet, &universal, &universal, &universal)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &universal, &universal, &universal)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &universal, &empty, &empty)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &empty, &universal, &empty)); ASSERT_TRUE(checkCaseNew(&unionInSet, &universal, &empty, &universal)); ASSERT_TRUE(checkCaseNew(&unionInSet, &empty, &universal, &universal)); ASSERT_TRUE(checkCaseNew(&unionInSet, &empty, &empty, &empty)); ASSERT_TRUE(checkCaseNew(&subtractFromSet, &empty, &empty, &empty)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &empty, &empty, &empty)); createEmptySet(&s); ASSERT_TRUE(isEmptySet(&s)); for (i = 0; i < 10; i++) { insertSet(&s, i); } ASSERT_TRUE(s.len == 10); for (i = 0; i < 10; i++) { ASSERT_TRUE(isMemberSet(&s, i)); } for (i = 0; i < 10; i++) { removeSet(&s, i); removeSet(&s, i); ASSERT_TRUE(s.len == 9 - i); } ASSERT_TRUE(isEmptySet(&s)); for (i = 0; i < NUM_TESTS; i++) { randomSet(&s); ASSERT_TRUE(isSubsetOf(&empty, &s)); ASSERT_FALSE(isSubsetOf(&s, &empty)); ASSERT_TRUE(isSubsetOf(&s, &universal)); ASSERT_FALSE(isSubsetOf(&universal, &s)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &empty, &s, &empty)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &s, &empty, &empty)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &universal, &s, &s)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &s, &universal, &s)); ASSERT_TRUE(checkCaseNew(&unionInSet, &universal, &s, &universal)); ASSERT_TRUE(checkCaseNew(&unionInSet, &s, &universal, &universal)); ASSERT_TRUE(checkCaseNew(&subtractFromSet, &s, &empty, &s)); assignSet(&r, &universal); subtractFromSet(&r, &s); ASSERT_TRUE(checkCaseNew(&subtractFromSet, &universal, &r, &s)); ASSERT_TRUE(checkCaseNew(&unionInSet, &s, &r, &universal)); ASSERT_TRUE(checkCaseNew(&unionInSet, &r, &s, &universal)); } destroySet(&empty); destroySet(&universal); destroySet(&s); destroySet(&r); } }