Example #1
0
    Status ChunkType::validate() const {

        if (!_name.is_initialized() || _name->empty()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << name.name() << " field");
        }

        if (!_ns.is_initialized() || _ns->empty()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << ns.name() << " field");
        }

        if (!_min.is_initialized() || _min->isEmpty()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << min.name() << " field");
        }

        if (!_max.is_initialized() || _max->isEmpty()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << max.name() << " field");
        }

        if (!_version.is_initialized() || !_version->isSet()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << version.name() << " field");
        }

        if (!_shard.is_initialized() || _shard->empty()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << shard.name() << " field");
        }

        // NOTE: all the following semantic checks should eventually become the caller's
        // responsibility, and should be moved out of this class completely

        // 'min' and 'max' must share the same fields.
        if (_min->nFields() != _max->nFields()) {
            return Status(ErrorCodes::FailedToParse,
                          str::stream() << "min and max have a different number of keys");
        }
        BSONObjIterator minIt(getMin());
        BSONObjIterator maxIt(getMax());
        while (minIt.more() && maxIt.more()) {
            BSONElement minElem = minIt.next();
            BSONElement maxElem = maxIt.next();
            if (strcmp(minElem.fieldName(), maxElem.fieldName())) {
                return Status(ErrorCodes::FailedToParse,
                              str::stream() << "min and max must have the same set of keys");
            }
        }

        // 'max' should be greater than 'min'.
        if (_min->woCompare(getMax()) >= 0) {
            return Status(ErrorCodes::FailedToParse,
                          str::stream() << "max key must be greater than min key");
        }

        return Status::OK();
    }
Example #2
0
    Status ChunkType::validate() const {

        if (!_name.is_initialized() || _name->empty()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << name.name() << " field");
        }

        if (!_ns.is_initialized() || _ns->empty()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << ns.name() << " field");
        }

        if (!_min.is_initialized() || _min->isEmpty()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << min.name() << " field");
        }

        if (!_max.is_initialized() || _max->isEmpty()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << max.name() << " field");
        }

        if (!_version.is_initialized() || !_version->isSet()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << version.name() << " field");
        }

        if (!_shard.is_initialized() || _shard->empty()) {
            return Status(ErrorCodes::NoSuchKey,
                          str::stream() << "missing " << shard.name() << " field");
        }

        // 'min' and 'max' must share the same fields.
        if (_min->nFields() != _max->nFields()) {
            return Status(ErrorCodes::BadValue,
                          str::stream() << "min and max have a different number of keys");
        }

        BSONObjIterator minIt(getMin());
        BSONObjIterator maxIt(getMax());
        while (minIt.more() && maxIt.more()) {
            BSONElement minElem = minIt.next();
            BSONElement maxElem = maxIt.next();
            if (strcmp(minElem.fieldName(), maxElem.fieldName())) {
                return Status(ErrorCodes::BadValue,
                              str::stream() << "min and max must have the same set of keys");
            }
        }

        // 'max' should be greater than 'min'.
        if (_min->woCompare(getMax()) >= 0) {
            return Status(ErrorCodes::BadValue,
                          str::stream() << "max key must be greater than min key");
        }

        return Status::OK();
    }
Example #3
0
Status ChunkType::validate() const {
    if (!_min.is_initialized() || _min->isEmpty()) {
        return Status(ErrorCodes::NoSuchKey, str::stream() << "missing " << min.name() << " field");
    }

    if (!_max.is_initialized() || _max->isEmpty()) {
        return Status(ErrorCodes::NoSuchKey, str::stream() << "missing " << max.name() << " field");
    }

    if (!_version.is_initialized() || !_version->isSet()) {
        return Status(ErrorCodes::NoSuchKey, str::stream() << "missing version field");
    }

    if (!_shard.is_initialized() || !_shard->isValid()) {
        return Status(ErrorCodes::NoSuchKey,
                      str::stream() << "missing " << shard.name() << " field");
    }

    // 'min' and 'max' must share the same fields.
    if (_min->nFields() != _max->nFields()) {
        return {ErrorCodes::BadValue,
                str::stream() << "min and max don't have the same number of keys: " << *_min << ", "
                              << *_max};
    }

    BSONObjIterator minIt(getMin());
    BSONObjIterator maxIt(getMax());
    while (minIt.more() && maxIt.more()) {
        BSONElement minElem = minIt.next();
        BSONElement maxElem = maxIt.next();
        if (strcmp(minElem.fieldName(), maxElem.fieldName())) {
            return {ErrorCodes::BadValue,
                    str::stream() << "min and max don't have matching keys: " << *_min << ", "
                                  << *_max};
        }
    }

    // 'max' should be greater than 'min'.
    if (_min->woCompare(getMax()) >= 0) {
        return {ErrorCodes::BadValue,
                str::stream() << "max is not greater than min: " << *_min << ", " << *_max};
    }

    return Status::OK();
}
int main(void) {
	const int magic_number = 13017;

	/* test min */
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	printf("the smallest of the first 10 natural numbers is: %d\n", minIt(a, 10));
	printf("the smallest of the first 10 natural numbers is: %d\n", minRec1(a, 10));
	printf("the smallest of the first 10 natural numbers is: %d\n", minRec2(a, 10));
	a[3] = -1;
	printf("now the smallest is %d\n", minIt(a, 10));
	printf("now the smallest is %d\n", minRec1(a, 10));
	printf("now the smallest is %d\n", minRec2(a, 10));
	
	/* test sqrt */
	printf("the sqrt of 25 is %g\n", sqrtIt(25.0, 0, 25.0));
	printf("the sqrt of 26 is %g\n", sqrtIt(26.0, 0, 26.0));
	printf("the sqrt of 2 is %g\n", sqrtIt(2.0, 0, 2.0));
	printf("the sqrt of 25 is %g\n", sqrtRec(25.0, 0, 25.0));
	printf("the sqrt of 26 is %g\n", sqrtRec(26.0, 0, 26.0));
	printf("the sqrt of 2 is %g\n", sqrtRec(2.0, 0, 2.0));

	/* test strCompare */
	char* s1; char* s2;
	s1 = "apple"; s2 = "apricot";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}
			
	s1 = "Apple"; s2 = "apple";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "baby boy"; s2 = "banana";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "a rather silly string"; s2 = "a rather silly string";
	if (strCompare(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "12345"; s2 = "12345";
	if (strCompare(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "Numbers: 12345"; s2 = "12345";
	if (strCompare(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	s1 = "Texas"; s2 = "California";
	if (strCompare(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	/* test strCompare2 */
	s1 = "apple"; s2 = "Apricot";
	if (strCompare2(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "Batman!"; s2 = "bat man";
	if (strCompare2(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "OMG, WTF?"; s2 = "oh my goodness, what the heck?";
	if (strCompare2(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	/* test maze */		
	srand(magic_number);
	makeMaze();
	recodeMaze();
	printf("recursive solution to the maze\n");
	printMaze();
	solveMazeRec(0, start_col);	
	printMaze();
	printf("\n\n\n");
	
	printf("iterative solution to the maze\n");
	srand(magic_number);
	makeMaze();
	recodeMaze();
	printMaze();
	solveMazeIt(0, start_col);
	printMaze();


	/* test Martian */
	Martian change1 = changeIt(15);
	printf("change 1 should be 0d, 3n, 0p and is: %dd %dn %dp\n", change1.dodeks, change1.nicks, change1.pennies);

	Martian change2 = changeIt(0);
	printf("change 2 should be 0d, 0n, 0p and is: %dd %dn %dp\n", change2.dodeks, change2.nicks, change2.pennies);

	Martian change3 = changeIt(17);
	printf("change 3 should be 1d, 1n, 0p and is: %dd %dn %dp\n", change3.dodeks, change3.nicks, change3.pennies);

	Martian change4 = changeIt(25);
	printf("change 4 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change4.dodeks, change4.nicks, change4.pennies);

	/* A very simple and obvious test of the general form of Martian
	 * be sure and test your solution more thoroughly!!!! */
	change4 = changeIt(25, 5, 12);
	printf("change 4 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change4.dodeks, change4.nicks, change4.pennies);
}