TEST(Test03, RelationalTests) { { Set s; Set t; createEmptySet(&s); createEmptySet(&t); for (int i = 0; i < NUM_TESTS; i++) { randomSet(&s); reassignSet(&t, &s); ASSERT_TRUE(isSubsetOf(&s, &t)); ASSERT_TRUE(isSubsetOf(&t, &s)); ASSERT_TRUE(isEqualToSet(&s, &t)); ASSERT_TRUE(isEqualToSet(&t, &s)); insertSet(&s, rand() % MAX_SET_SIZE + MAX_SET_SIZE); ASSERT_TRUE(isSubsetOf(&t, &s)); ASSERT_FALSE(isSubsetOf(&s, &t)); } destroySet(&s); destroySet(&t); } }
bool isSubsetOf(const MatchExpression* lhs, const MatchExpression* rhs) { invariant(lhs); invariant(rhs); if (lhs->equivalent(rhs)) { return true; } if (rhs->matchType() == MatchExpression::AND) { // 'lhs' must match a subset of the documents matched by each clause of 'rhs'. for (size_t i = 0; i < rhs->numChildren(); i++) { if (!isSubsetOf(lhs, rhs->getChild(i))) { return false; } } return true; } if (lhs->matchType() == MatchExpression::AND) { // At least one clause of 'lhs' must match a subset of the documents matched by 'rhs'. for (size_t i = 0; i < lhs->numChildren(); i++) { if (isSubsetOf(lhs->getChild(i), rhs)) { return true; } } return false; } if (lhs->matchType() == MatchExpression::OR) { // Every clause of 'lhs' must match a subset of the documents matched by 'rhs'. for (size_t i = 0; i < lhs->numChildren(); i++) { if (!isSubsetOf(lhs->getChild(i), rhs)) { return false; } } return true; } if (isComparisonMatchExpression(rhs)) { return _isSubsetOf(lhs, static_cast<const ComparisonMatchExpression*>(rhs)); } if (rhs->matchType() == MatchExpression::EXISTS) { return _isSubsetOf(lhs, static_cast<const ExistsMatchExpression*>(rhs)); } return false; }
void Java_com_facebook_samples_sessionlogin_SessionLoginSampleActivity_nativePublishStory(JNIEnv *env, jobject objectRef, jobject androidContext) { LOGV("Java_com_facebook_samples_sessionlogin_SessionLoginSampleActivity_nativePublishStory enter"); toastChars("Publishing Story..."); std::vector<Object *> permissions_list; Object *publish_permission = (Object *) ((CharSequence *) new String(to_std_vector("publish_actions"))); permissions_list.push_back(publish_permission); LOGV("Invoking asList"); PERMISSIONS = Arrays::asList(permissions_list); Session *session = Session::getActiveSession(); List *permissions = session->getPermissions(); if (!isSubsetOf(*PERMISSIONS, *permissions)) { pendingPublishReauthorization = true; Session_NewPermissionsRequest *newPermissionsRequest = new Session_NewPermissionsRequest(*activity, *PERMISSIONS); session->requestNewPublishPermissions(*newPermissionsRequest); delete newPermissionsRequest; } else { pendingPublishReauthorization = false; publish_story(); } delete permissions; delete session; delete PERMISSIONS; delete publish_permission; LOGV("Java_com_facebook_samples_sessionlogin_SessionLoginSampleActivity_nativePublishStory exit"); }
void Java_com_facebook_samples_sessionlogin_SessionLoginSampleActivity_nativeGetUserAttr(JNIEnv *env, jobject objectRef, jobject androidContext) { LOGV("Java_com_facebook_samples_sessionlogin_SessionLoginSampleActivity_nativeGetUserAttr enter"); toastChars("Getting Bithday..."); std::vector<Object *> permissions_list; Object *birthday_permission = (Object *) ((CharSequence *) new String(to_std_vector("user_birthday"))); permissions_list.push_back(birthday_permission); LOGV("Invoking asList"); PERMISSIONS = Arrays::asList(permissions_list); Session *session = Session::getActiveSession(); List *permissions = session->getPermissions(); if (!isSubsetOf(*PERMISSIONS, *permissions)) { pendingBirthdayReauthorization = true; Session_NewPermissionsRequest *newPermissionsRequest = new Session_NewPermissionsRequest(*activity, *PERMISSIONS); session->requestNewReadPermissions(*newPermissionsRequest); delete newPermissionsRequest; } else { pendingBirthdayReauthorization = false; get_user_birthday(); } delete permissions; delete session; delete PERMISSIONS; delete birthday_permission; LOGV("Java_com_facebook_samples_sessionlogin_SessionLoginSampleActivity_nativeGetUserAttr exit"); }
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); }
/* add all elements of other to self (obviously, without creating duplicate elements) */ void unionInSet(Set* self, const Set* other) { if (isEmptySet(other)) { return; } //adding nothing to self set if (isEqualToSet (self, other)) { return; } //same set, nothing to add if (isSubsetOf (other, self)) { return; } //everything in other already in self if (self == other) { return; } //same set, nothing to add if (self->len == 0) { createCopySet (self, other); } else { Set copy_set; createCopySet (©_set, self); copy_set.capacity = copy_set.len + other->len; destroySet (self); self->elements = (int*) malloc (copy_set.capacity * sizeof (int)); //self now has room to union two unique sets int copy_index = 0; int other_index = 0; int self_index = 0; while (copy_index < copy_set.len || other_index < other->len) { if (copy_set.elements[copy_index] == other->elements[other_index]) { self->elements[self_index] = copy_set.elements[copy_index]; other_index += 1; copy_index += 1; } else if ((other_index >= other->len || copy_set.elements[copy_index] < other->elements[other_index]) && copy_index < copy_set.len) { self->elements[self_index] = copy_set.elements[copy_index]; copy_index += 1; } else if ((copy_index >= copy_set.len || copy_set.elements[copy_index] > other->elements[other_index]) && other_index < other->len) { self->elements[self_index] = other->elements[other_index]; other_index += 1; } self_index += 1; } self->len = self_index; self->capacity = copy_set.capacity; destroySet (©_set); } //might have to recreate insert code/subset code here /*code here*/ //have two sets (one a copy of self), depending on which number is lower of the two sets at their respective indicies, put that one first into self //assert (checkValidSet (self)); //test code, remove later }
/* remove all elements from self that are not also elements of other */ void intersectFromSet(Set* self, const Set* other) { if (isEmptySet(self)) { return; } //can't remove anything from empty set if (isEqualToSet (self, other)) { return; } //all elements aready in common if (isSubsetOf (self, other)) { return; } //everything in self is in other, nothing to remove if (self == other) { return; } //all elements already in common /*code here*/ //have two sets (one a copy of self), if they don't match increase other index until they do, if they match add to self and increase both indicies //might have to recreate remove code/subset code here Set copy_set; createCopySet (©_set, self); copy_set.capacity = copy_set.len; destroySet (self); self->elements = (int*) malloc (copy_set.capacity * sizeof (int)); //copy original set into copy_set and rebuild self int copy_index = 0; int other_index = 0; int self_index = 0; while (copy_index < copy_set.len && other_index < other->len) { if (copy_set.elements[copy_index] < other->elements[other_index]) { copy_index += 1; } else if (copy_set.elements[copy_index] > other->elements[other_index]) { other_index += 1; } else if (copy_set.elements[copy_index] == other->elements[other_index]) { self->elements[self_index] = copy_set.elements[copy_index]; self_index += 1; copy_index += 1; other_index += 1; } } self->len = self_index; self->capacity = copy_set.capacity; destroySet (©_set); //assert (checkValidSet (self)); //test code, remove later }
/* remove all elements from self that are also elements of other */ void subtractFromSet(Set* self, const Set* other) { if (isEmptySet(self)) { return; } //can't remove anything from empty set if (isEmptySet(other)) { return; } //nothing to remove from self if (isSubsetOf (self, other)) { createEmptySet (self); return; } /*code here*/ //have two sets (one a copy of self), //might have to recreate remove code/subset code here /*Set intersect_set; createCopySet (&intersect_set, self); intersect_set.capacity = intersect_set.len; intersectFromSet (&intersect_set, other); //create temporary set with intersection of self and other */ Set copy_set; createCopySet (©_set, self); copy_set.capacity = copy_set.len; destroySet (self); self->elements = (int*) malloc (copy_set.capacity * sizeof (int)); //copy original set into copy_set and rebuild self /*int intersect_index = 0; int self_index = 0; int copy_index = 0; while (copy_index < copy_set.len) { if (copy_set.elements[copy_index] == intersect_set.elements[intersect_index]) { intersect_index += 1; copy_index += 1; } else if (intersect_index >= intersect_set.len || copy_set.elements[copy_index] < intersect_set.elements[intersect_index]) { self->elements[self_index] = copy_set.elements[copy_index]; copy_index += 1; self_index += 1; } else if (copy_set.elements[copy_index] > intersect_set.elements[intersect_index]) { intersect_index += 1; } }*/ int copy_index = 0; int self_index = 0; int other_index = 0; while (copy_index < copy_set.len) { if (other_index >= other->len || copy_set.elements[copy_index] < other->elements[other_index]) { self->elements[self_index] = copy_set.elements[copy_index]; self_index += 1; copy_index += 1; } else if (copy_set.elements[copy_index] > other->elements[other_index]) { other_index += 1; } else { copy_index += 1; other_index += 1; } } self->len = self_index; self->capacity = copy_set.capacity; destroySet (©_set); /*destroySet (&intersect_set);*/ //assert (checkValidSet (self)); //test code, remove later }
/// Equality bool isEqual(const tuple_computer_base& x2)const { return isSubsetOf(x2) && x2.isSubsetOf(*this); }
void isSubsetTimeFun(void) { bogus_value = isSubsetOf(setA, setAPrime); }
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(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); } }
bool StructureSet::operator==(const StructureSet& other) const { if (size() != other.size()) return false; return isSubsetOf(other); }