Example #1
0
static void test_st_randomInt(CuTest *testCase) {
    /*
     * Exercises the random int function.
     */
    int32_t min = INT32_MIN;
    int32_t max = INT32_MAX;
    int32_t v;
    for (int32_t i = -9; i < 10; ++i) {
        CuAssertTrue(testCase, st_randomInt(i, i + 1) == i);
    }
    for(int64_t i = 0; i < 100000; i++) {
        v = st_randomInt(min, max);
        CuAssertTrue(testCase, v >= min);
        CuAssertTrue(testCase, v < max);
    }
    min = -1;
    max = 10;
    for(int64_t i = 0; i < 100000; i++) {
        v = st_randomInt(min, max);
        CuAssertTrue(testCase, v >= min);
        CuAssertTrue(testCase, v < max);
    }
    stTry {
        st_randomInt(1, 1);
    } stCatch(except) {
        CuAssertTrue(testCase, stExcept_getId(except) == RANDOM_EXCEPTION_ID);
    }
    stTryEnd
}
Example #2
0
static void test_stSortedSet_searchGreaterThan(CuTest* testCase) {
    sonLibSortedSetTestSetup();

    for(int32_t i=0; i<size; i++) {
        stSortedSet_insert(sortedSet, stIntTuple_construct(1, input[i]));
    }
    //static int32_t sortedInput[] = { -10, -1, 1, 3, 5, 10, 12 };
    CuAssertTrue(testCase, stSortedSet_searchGreaterThan(sortedSet,
                 stIntTuple_construct(1, -11)) == stSortedSet_search(sortedSet, stIntTuple_construct(1, -10)));
    CuAssertTrue(testCase, stSortedSet_searchGreaterThan(sortedSet,
                 stIntTuple_construct(1, -10)) == stSortedSet_search(sortedSet, stIntTuple_construct(1, -1)));
    CuAssertTrue(testCase, stSortedSet_searchGreaterThan(sortedSet,
                 stIntTuple_construct(1, -5)) == stSortedSet_search(sortedSet, stIntTuple_construct(1, -1)));
    CuAssertTrue(testCase, stSortedSet_searchGreaterThan(sortedSet,
                 stIntTuple_construct(1, 1)) == stSortedSet_search(sortedSet, stIntTuple_construct(1, 3)));
    CuAssertTrue(testCase, stSortedSet_searchGreaterThan(sortedSet,
                 stIntTuple_construct(1, 13)) == NULL);
    CuAssertTrue(testCase, stSortedSet_searchGreaterThan(sortedSet,
                 stIntTuple_construct(1, 12)) == NULL);

    for(int32_t i=0; i<100; i++) {
        stSortedSet_insert(sortedSet, stIntTuple_construct(1, st_randomInt(-1000, 1000)));
    }
    stList *list = stSortedSet_getList(sortedSet);
    for(int32_t i=1; i<stList_length(list); i++) {
        stIntTuple *p = stList_get(list, i-1);
        stIntTuple *j = stList_get(list, i);
        stIntTuple *k = stIntTuple_construct(1, st_randomInt(stIntTuple_getPosition(p, 0), stIntTuple_getPosition(j, 0)));
        CuAssertTrue(testCase, stSortedSet_searchGreaterThan(sortedSet, k) == j);
        stIntTuple_destruct(k);
    }
    stList_destruct(list);

    sonLibSortedSetTestTeardown();
}
Example #3
0
static stList *getRandomPairwiseAlignments() {
    stList *pairwiseAlignments = stList_construct3(0, (void(*)(void *)) destructPairwiseAlignment);
    int64_t randomAlignmentNumber = st_randomInt(0, 10);
    for (int64_t i = 0; i < randomAlignmentNumber; i++) {
        char *contig1 = stString_print("%" PRIi64 "", i);
        char *contig2 = stString_print("%" PRIi64 "", i * 10);
        int64_t start1 = st_randomInt(100000, 1000000);
        int64_t start2 = st_randomInt(100000, 1000000);
        int64_t strand1 = st_random() > 0.5;
        int64_t strand2 = st_random() > 0.5;
        int64_t end1 = start1;
        int64_t end2 = start2;
        struct List *operationList = constructEmptyList(0, NULL);
        while (st_random() > 0.1) {
            int64_t length = st_randomInt(0, 10);
            int64_t type = st_randomInt(0, 3);
            assert(type < 3);
            listAppend(operationList, constructAlignmentOperation(type, length, 0));
            if (type != PAIRWISE_INDEL_Y) {
                end1 += strand1 ? length : -length;
            }
            if (type != PAIRWISE_INDEL_X) {
                end2 += strand2 ? length : -length;
            }
        }
        stList_append(pairwiseAlignments,
                      constructPairwiseAlignment(contig1, start1, end1, strand1, contig2, start2, end2, strand2, 0.0, operationList));
        free(contig1);
        free(contig2);
    }
    return pairwiseAlignments;
}
static void testCalculateZScore(CuTest *testCase) {
    for(int64_t i=0; i<100; i++) {
        int64_t n = st_randomInt(0, 100);
        int64_t m = st_randomInt(0, 100);
        int64_t k = st_randomInt(1, 10000);
        double theta = st_random() > 0.05 ? st_random() : 0.0;
        double zScore = calculateZScore(n, m, k, theta);
        double zScoreSlow = calculateZScoreSlow(n, m, k, theta);
        st_logDebug("The slow computed score: %f the fast computed score: %f, n: %" PRIi64 " m: %" PRIi64 " k: %" PRIi64 ", theta: %lf\n", zScoreSlow, zScore, n, m, k, theta);
        CuAssertDblEquals(testCase, zScoreSlow, zScore, 0.000001);
    }
}
static void setup() {
    teardown();
    assert(nodeNumber == -1);
    while(nodeNumber % 2 != 0) {
        nodeNumber = st_randomInt(0, 100);
    }
    assert(nodeNumber >= 0);
    assert(nodeNumber % 2 == 0);
    stubs = stList_construct3(0, (void (*)(void *))stIntTuple_destruct);
    chains = stList_construct3(0, (void (*)(void *))stIntTuple_destruct);
    for(int64_t i=0; i<nodeNumber/2; i++) {
        assert(nodeNumber/2 > 0);
        stIntTuple *edge = stIntTuple_construct2(i, nodeNumber/2 + i);
        if(stList_length(stubs) == 0 || st_random() > 0.9) {
            stList_append(stubs, edge);
        }
        else {
            stList_append(chains, edge);
        }
    }
    zMatrix = st_calloc(nodeNumber*nodeNumber, sizeof(float));
    for(int64_t i=0; i<nodeNumber; i++) {
        for(int64_t j=i+1; j<nodeNumber; j++) {
            double score = st_random();
            zMatrix[i * nodeNumber + j] = score;
            zMatrix[j * nodeNumber + i] = score;
        }
    }
    st_logDebug("To test the adjacency problem we've created a problem with %" PRIi64 " nodes %" PRIi64 " stubs and %" PRIi64 " chains\n", nodeNumber, stList_length(stubs), stList_length(chains));
}
Example #6
0
static void test_st_randomInt_distribution_0(CuTest *testCase) {
    /*
     * check the distribution of the randomInt function
     */
    double med, var, mu = 0.0;
    uint32_t n = 1000000;
    uint32_t reps = 50;
    int32_t min = 0;
    int32_t max = 101;
    int32_t *array = NULL;
    for (uint32_t j = 0; j < reps; ++j) {
        array = (int32_t *) st_malloc(sizeof(*array) * n);
        for (int32_t i = 0; i < n; i++) {
            array[i] = st_randomInt(min, max);
            mu = runningMean32(mu, array[i], i);
        }
        var = sv32(array, n, mu);
        med = median32(array, n); // median() will sort array
        CuAssertTrue(testCase, array[0] <= array[n - 1]);
        CuAssertTrue(testCase, array[0] < max);
        CuAssertTrue(testCase, array[n - 1] < max);
        // median of discrete uniform is
        // median = (a + b) / 2
        CuAssertTrue(testCase, med == 50.0);
        // mean of discrete uniform is
        // mu = (a + b) / 2
        CuAssertTrue(testCase, mu > 0.9 * (max - 1) / 2.0);
        CuAssertTrue(testCase, mu < 1.1 * (max - 1) / 2.0);
        // variance of discrete uniform is
        // var = ((b - a + 1)^2 - 1) / 12
        CuAssertTrue(testCase, var < 1.1 * (((max - min + 1) * (max - min + 1) - 1) / 12.0));
        CuAssertTrue(testCase, var > 0.9 * (((max - min + 1) * (max - min + 1) - 1) / 12.0));
        free(array);
    }
}
Example #7
0
/*
 * Retrieves really long records from the database.
 */
static void bigRecordRetrieval(CuTest *testCase) {
    setup();
    for (int32_t i = 0; i < 10; i++) {
        int32_t size = st_randomInt(5000000, 10000000);
        char *randomRecord = st_malloc(size * sizeof(char));
        for (int32_t j = 0; j < size; j++) {
            randomRecord[j] = (char) st_randomInt(0, 100);
        }

        //st_uglyf("I am inserting record %i %i\n", i, size);
        stKVDatabase_insertRecord(database, i, randomRecord, size * sizeof(char));

        //st_uglyf("I am creating the record %i %i\n", i, size);
        //Check they are equivalent.
        int64_t size2;
        char *randomRecord2 = stKVDatabase_getRecord2(database, i, &size2);
        CuAssertTrue(testCase, size == size2);
        for (int32_t j = 0; j < size; j++) {
            CuAssertTrue(testCase, randomRecord[j] == randomRecord2[j]);
        }
    }
    teardown();
}
static void setup() {
    teardown();

    //Make nodes
    nodes = stList_construct3(0, (void(*)(void *)) stIntTuple_destruct);
    int64_t nodeNumber = st_randomInt(0, 1000);
    for (int64_t i = 0; i < nodeNumber; i++) {
        stList_append(nodes, stIntTuple_construct1( i));
    }

    //Make edges
    edges = stList_construct3(0, (void(*)(void *)) stIntTuple_destruct);
    float edgeProb = st_random();
    for (int64_t i = 0; i < nodeNumber; i++) {
        for (int64_t j = i; j < nodeNumber; j++) {
            if (st_random() <= edgeProb) {
                stList_append(edges, stIntTuple_construct3( st_randomInt(1, 100), i, j));
            }
        }
    }

    //Max component size
    maxComponentSize = 1 + log(nodeNumber) * 10; //(st_randomInt(0, nodeNumber+1);
}
Example #9
0
static void test_stPosetAlignment_addAndIsPossible(CuTest *testCase) {
    for(int64_t trial=0; trial<100; trial++) {
        setup();

        //Make random number of sequences.
        stList *sequenceLengths = stList_construct3(0, (void (*)(void *))stIntTuple_destruct);
        for(int64_t i=0; i<sequenceNumber; i++) {
            stList_append(sequenceLengths, stIntTuple_construct1( st_randomInt(0, MAX_SEQUENCE_SIZE)));
        }

        //Propose random alignment pairs...
        stList *pairs = stList_construct3(0, (void(*)(void *))stIntTuple_destruct);
        int64_t maxAlignedPairs = st_randomInt(0, MAX_ALIGNMENTS);
        if(sequenceNumber > 0) {
            for(int64_t i=0; i<maxAlignedPairs; i++) {
                int64_t seq1 = st_randomInt(0, sequenceNumber);
                int64_t seqLength1 = stIntTuple_get(stList_get(sequenceLengths, seq1), 0);
                if(seqLength1 == 0) {
                    continue;
                }
                int64_t position1 = st_randomInt(0, seqLength1);
                int64_t seq2 = st_randomInt(0, sequenceNumber);
                int64_t seqLength2 = stIntTuple_get(stList_get(sequenceLengths, seq1), 0);
                if(seqLength2 == 0) {
                    continue;
                }
                int64_t position2 = st_randomInt(0, seqLength2);
                if(seq1 != seq2) {
                    stList_append(pairs, stIntTuple_construct4( seq1, position1, seq2, position2));
                    if(stPosetAlignment_isPossible(posetAlignment, seq1, position1, seq2, position2)) {
                        st_logInfo("In %" PRIi64 " %" PRIi64 " %" PRIi64 " %" PRIi64 " \n", seq1, position1, seq2, position2);
                        //For each accepted pair check it doesn't create a cycle.
                        CuAssertTrue(testCase, !containsACycle(pairs, sequenceNumber));
                        CuAssertTrue(testCase, stPosetAlignment_add(posetAlignment, seq1, position1, seq2, position2));
                    }
                    else {
                        st_logInfo("Out %" PRIi64 " %" PRIi64 " %" PRIi64 " %" PRIi64 " \n", seq1, position1, seq2, position2);
                        //For each rejected pair check it creates a cycle..
                        CuAssertTrue(testCase, containsACycle(pairs, sequenceNumber));
                        CuAssertTrue(testCase, !stPosetAlignment_isPossible(posetAlignment, seq1, position1, seq2, position2));
                        stIntTuple_destruct(stList_pop(pairs)); //remove the pair which created the cycle.
                        CuAssertTrue(testCase, !containsACycle(pairs, sequenceNumber)); //Check we're back to being okay..
                    }
                }
            }
        }

        //Cleanup
        stList_destruct(sequenceLengths);
        stList_destruct(pairs);
        teardown();
        st_logInfo("Passed a random ordering test with %" PRIi64 " sequences and %" PRIi64 " aligned pairs\n", sequenceNumber, maxAlignedPairs);
    }
}
Example #10
0
void testCactusDisk_getUniqueID_UniqueIntervals(CuTest* testCase) {
    cactusDiskTestSetup();
    stSortedSet *uniqueNames = stSortedSet_construct3(testCactusDisk_getUniqueID_UniqueP, free);
    for (int64_t i = 0; i < 10; i++) { //Gets a billion ids, checks we are good.
        int64_t intervalSize = st_randomInt(0, 100000);
        Name uniqueName = cactusDisk_getUniqueIDInterval(cactusDisk, intervalSize);
        for(int64_t j=0; j<intervalSize; j++) {
            CuAssertTrue(testCase, uniqueName > 0);
            CuAssertTrue(testCase, uniqueName < INT64_MAX);
            CuAssertTrue(testCase, uniqueName != NULL_NAME);
            char *cA = cactusMisc_nameToString(uniqueName);
            CuAssertTrue(testCase, stSortedSet_search(uniqueNames, cA) == NULL);
            CuAssertTrue(testCase, cactusMisc_stringToName(cA) == uniqueName);
            stSortedSet_insert(uniqueNames, cA);
            uniqueName++;
        }
    }
    stSortedSet_destruct(uniqueNames);
    cactusDiskTestTeardown();
}
static void testGibbsSamplingWithSimulatedAnnealing(CuTest *testCase,
        double(*temperatureFn)(double), bool pureGreedy, int64_t maxNumberOfChainsBeforeSwitchingToFast) {
    for (int64_t i = 0; i < 100; i++) {
        setup();
        double totalScore;
        time_t startTime = time(NULL);
        bool fast = maxNumberOfChainsBeforeSwitchingToFast > nodeNumber;
        stList *reference = makeReferenceGreedily(stubs, chains, zMatrix,
                nodeNumber, &totalScore, fast);
        checkIsValidReference(testCase, reference, totalScore);
        time_t totalTime = time(NULL) - startTime;
        char *cA = stString_print("Pre-annealing greedy (fast:%" PRIi64 "), %" PRIi64 " nodes, it took %" PRIi64 " seconds, score: %f\n", fast, nodeNumber, totalTime, totalScore);
        st_logInfo(cA);
        logReference(reference, nodeNumber, zMatrix, totalScore, cA);
        free(cA);
        startTime = time(NULL);
        int64_t permutations = st_randomInt(0, 100);
        if(pureGreedy) {
            greedyImprovement(reference, chains, zMatrix, permutations);
        }
        else {
            gibbsSamplingWithSimulatedAnnealing(reference, chains, zMatrix,
                    permutations, temperatureFn);
        }
        double totalScoreAfterAnnealing = calculateZScoreOfReference(reference, nodeNumber, zMatrix);
        checkIsValidReference(testCase, reference, totalScoreAfterAnnealing);
        totalTime = time(NULL) - startTime;
        cA = stString_print("Post-annealing permutations:%" PRIi64 ", %" PRIi64 " nodes, it took %" PRIi64 " seconds, score: %f\n", permutations, nodeNumber, totalTime, totalScore);
        st_logInfo(cA);
        logReference(reference, nodeNumber, zMatrix, totalScoreAfterAnnealing,
                cA);
        if(pureGreedy) {
            CuAssertTrue(testCase, totalScoreAfterAnnealing + 0.001 >= totalScore);
        }
        teardown();
    }
}
Example #12
0
static void readWriteAndRemoveRecordsLotsIteration(CuTest *testCase, int numRecords, bool reopenDatabase) {
    //Make a big old list of records..
    stSortedSet *set = stSortedSet_construct3((int(*)(const void *, const void *)) stIntTuple_cmpFn,
            (void(*)(void *)) stIntTuple_destruct);
    while (stSortedSet_size(set) < numRecords) {
        int32_t key = st_randomInt(0, 100 * numRecords);
        stIntTuple *tuple = stIntTuple_construct(1, key);
        if (stSortedSet_search(set, tuple) == NULL) {
            CuAssertTrue(testCase, !stKVDatabase_containsRecord(database, key));
            stSortedSet_insert(set, tuple);
            stKVDatabase_insertRecord(database, key, &key, sizeof(int32_t));
            CuAssertTrue(testCase, stKVDatabase_containsRecord(database, key));
        } else {
            CuAssertTrue(testCase, stKVDatabase_containsRecord(database, key));
            stIntTuple_destruct(tuple); // already in db
        }
    }

    readWriteAndRemoveRecordsLotsCheck(testCase, set, 1);

    //Update all records to negate values
    stSortedSetIterator *it = stSortedSet_getIterator(set);
    stIntTuple *tuple;
    while ((tuple = stSortedSet_getNext(it)) != NULL) {
        int32_t *value = (int32_t *) stKVDatabase_getRecord(database, stIntTuple_getPosition(tuple, 0));
        *value *= -1;
        stKVDatabase_updateRecord(database, stIntTuple_getPosition(tuple, 0), value, sizeof(int32_t));
        CuAssertTrue(testCase, stKVDatabase_containsRecord(database, stIntTuple_getPosition(tuple, 0)));
        free(value);
    }
    stSortedSet_destructIterator(it);

    readWriteAndRemoveRecordsLotsCheck(testCase, set, -1);

    //Try optionally committing the transaction and reloading the database..
    if (reopenDatabase) {
        //stKVDatabase_commitTransaction(database);
        stKVDatabase_destruct(database);
        database = stKVDatabase_construct(conf, false);
        //stKVDatabase_startTransaction(database);
    }

    //Now remove each one..
    it = stSortedSet_getIterator(set);
    while ((tuple = stSortedSet_getNext(it)) != NULL) {
        CuAssertTrue(testCase, stKVDatabase_containsRecord(database, stIntTuple_getPosition(tuple, 0)));
        stKVDatabase_removeRecord(database, stIntTuple_getPosition(tuple, 0));
        CuAssertTrue(testCase, !stKVDatabase_containsRecord(database, stIntTuple_getPosition(tuple, 0)));
        //Test we get exception if we remove twice.
        stTry {
                stKVDatabase_removeRecord(database, stIntTuple_getPosition(tuple, 0));
                CuAssertTrue(testCase, 0);
            }
            stCatch(except)
                {
                    CuAssertTrue(testCase, stExcept_getId(except) == ST_KV_DATABASE_EXCEPTION_ID);
                }stTryEnd;
    }
    stSortedSet_destructIterator(it);
    CuAssertIntEquals(testCase, 0, stKVDatabase_getNumberOfRecords(database));

    stSortedSet_destruct(set);
}
Example #13
0
static void partialRecordRetrieval(CuTest *testCase) {
    setup();

    //Make some number of large records
    stList *records = stList_construct3(0, free);
    stList *recordSizes = stList_construct3(0, (void(*)(void *)) stIntTuple_destruct);
    for (int32_t i = 0; i < 300; i++) {
        int32_t size = st_randomInt(0, 80);
        size = size * size * size; //Use cubic size distribution
        char *randomRecord = st_malloc(size * sizeof(char));
        for (int32_t j = 0; j < size; j++) {
            randomRecord[j] = (char) st_randomInt(0, 100);
        }
        stList_append(records, randomRecord);
        stList_append(recordSizes, stIntTuple_construct(1, size));
        CuAssertTrue(testCase, !stKVDatabase_containsRecord(database, i));
        stKVDatabase_insertRecord(database, i, randomRecord, size * sizeof(char));
        CuAssertTrue(testCase, stKVDatabase_containsRecord(database, i));
        //st_uglyf("I am creating the record %i %i\n", i, size);
    }

    while (st_random() > 0.001) {
        int32_t recordKey = st_randomInt(0, stList_length(records));
        CuAssertTrue(testCase, stKVDatabase_containsRecord(database, recordKey));

        char *record = stList_get(records, recordKey);
        int32_t size = stIntTuple_getPosition(stList_get(recordSizes, recordKey), 0);

        //Get partial record
        int32_t start = size > 0 ? st_randomInt(0, size) : 0;
        int32_t partialSize = size - start > 0 ? st_randomInt(start, size) - start : 0;
        assert(start >= 0);
        assert(partialSize >= 0);
        assert(partialSize + start <= size);
        //st_uglyf("I am getting record %i %i %i %i\n", recordKey, start, partialSize, size);
        char *partialRecord = stKVDatabase_getPartialRecord(database, recordKey, start * sizeof(char),
                partialSize * sizeof(char), size * sizeof(char));

        //Check they are equivalent..
        for (int32_t i = 0; i < partialSize; i++) {
            if (record[start + i] != partialRecord[i]) {
                st_uglyf("There was a difference %i %i for record %i %i\n", record[start + i], partialRecord[i], i,
                        partialSize);
            }
            //CuAssertTrue(testCase, record[start + i] == partialRecord[i]);
        }

        //Check we can not get out of bounds.. (start less than zero)
        stTry {
                stKVDatabase_getPartialRecord(database, recordKey, -1, 1, size * sizeof(char));
            }stCatch(except)
                {
                    CuAssertTrue(testCase, stExcept_getId(except) == ST_KV_DATABASE_EXCEPTION_ID);
                }stTryEnd;

        //Check we can not get out of bounds.. (start greater than index start)
        stTry {
                stKVDatabase_getPartialRecord(database, recordKey, size, 1, size * sizeof(char));
            }stCatch(except)
                {
                    CuAssertTrue(testCase, stExcept_getId(except) == ST_KV_DATABASE_EXCEPTION_ID);
                }stTryEnd;

        //Check we can not get out of bounds.. (total size if greater than record length)
        stTry {
                stKVDatabase_getPartialRecord(database, recordKey, 0, size + 1, size * sizeof(char));
            }stCatch(except)
                {
                    CuAssertTrue(testCase, stExcept_getId(except) == ST_KV_DATABASE_EXCEPTION_ID);
                }stTryEnd;

        //Check we can not get non existent record
        stTry {
                stKVDatabase_getPartialRecord(database, 1000000, 0, size, size * sizeof(char));
            }stCatch(except)
                {
                    CuAssertTrue(testCase, stExcept_getId(except) == ST_KV_DATABASE_EXCEPTION_ID);
                }stTryEnd;
    }

    stList_destruct(records);
    stList_destruct(recordSizes);

    teardown();
}
Example #14
0
static void setup() {
    teardown();
    sequenceNumber = st_randomInt(0, MAX_SEQUENCE_NUMBER);
    posetAlignment = stPosetAlignment_construct(sequenceNumber);
}