예제 #1
0
void test_seeding(unsigned int num)
{
    struct random *p1, *p2;
    unsigned int seed[] = { 1, 2, 3, 4 };
    int err;
    
    p1 = random_new();
    p2 = random_new();
    
    assert(p1);
    assert(p2);
    
    err = random_set_seed(p1, seed, ARRAY_SIZE(seed));
    assert(err == 0);
    
    err = random_set_seed(p2, seed, ARRAY_SIZE(seed));
    assert(err == 0);
    
    while(num--)
        assert(random_uint(p1) == random_uint(p2));
    
    random_delete(p1);
    random_delete(p2);
}
예제 #2
0
void test_randomness(void)
{
    struct random *rand;
    unsigned int n, i, num;
    
    rand = random_new();
    assert(rand);
    
    num = 100;
    
    for(i = 0; i < num; ++i) {
        n = random_uint(rand);
        
        fprintf(stdout, "%-10u\t", n);
        
        if((i % 3) == 0)
            fprintf(stdout, "\n");
    }
    
    fprintf(stdout, "\n");
    
    random_delete(rand);
}
TEST_F(FailureFixture, DeleteSomeDiskMessagesTest) {
    PriorityBuffer<PriorityMessage> buffer{get_priority};
    std::random_device generator;
    std::uniform_int_distribution<unsigned long long> distribution(0, 100LL);
    for (int i = 0; i < NUMBER_MESSAGES_IN_TEST; ++i) {
        auto message = std::unique_ptr<PriorityMessage>{ new PriorityMessage{} };
        auto priority = distribution(generator);
        message->set_priority(priority);
        EXPECT_TRUE(message->IsInitialized());
        EXPECT_EQ(priority, message->priority());
        buffer.Push(std::move(message));
    }
    unsigned long long number_to_delete = 0;
    unsigned long long number_of_files = 0;
    {
        number_of_files = number_of_files_();
        std::uniform_int_distribution<unsigned long long> random_delete(1, number_of_files);
        number_to_delete = random_delete(generator);
    }

    fs::directory_iterator begin(buffer_path_), end;
    std::vector<std::string> hashes;
    for (auto iterator = begin; iterator != end; ++iterator) {
        if (!(fs::is_directory(*iterator) || 
                iterator->path().filename().native().substr(0, 10) == "prism_data")) {
            if (hashes.size() >= number_to_delete) {
                break;
            }

            hashes.push_back(iterator->path().filename().native());
        }
    }

    for (auto& hash : hashes) {
        ASSERT_TRUE(fs::remove(buffer_path_ / fs::path{hash}));
    }
    {
        ASSERT_EQ(number_of_files - number_to_delete, number_of_files_());
    }

    unsigned long long priority = 100LL;
    for (int i = 0; i < (NUMBER_MESSAGES_IN_TEST - number_to_delete); ) {
        auto message = buffer.Pop();
        // Check if the message has been deleted or not
        if (message) {
            // If it is, add it to i
            EXPECT_GE(priority, message->priority());
            ++i;
            priority = message->priority();
        }
    }

    // There should be no more initialized messages, so every subsequent Pop should be bad
    for (int i = 0; i < NUMBER_MESSAGES_IN_TEST; ++i) {
        auto message = buffer.Pop();
        if (!message) {
            break;
        }
        std::cout << "i: " << message.get() << std::endl;
    }
}
예제 #4
0
void test_duplicates(unsigned int num)
{
    const struct map_config map_conf = {
        .size           = MAP_DEFAULT_SIZE,
        .lower_bound    = MAP_DEFAULT_LOWER_BOUND,
        .upper_bound    = MAP_DEFAULT_UPPER_BOUND,
        .static_size    = false,
        .key_compare    = &compare_int,
        .key_hash       = &hash_uint,
        .data_delete    = NULL,
    };
    struct map *m;
    struct random *random;
    unsigned int i, n, dups1, dups2;;
    int err;
    
    m = map_new(&map_conf);
    random = random_new();
    
    assert(m);
    assert(random);
    
    dups1 = 0;
    
    for(i = 0; i < num; ++i) {
        n = random_uint(random);
        
        if(map_contains(m, (void *)(long) n)) {
            dups1 += 1;
        } else {
            err = map_insert(m, (void *)(long) n, (void *)(long) n);
            if(err < 0) {
                fprintf(stderr, "Warning: "
                        "Map unable to insert %u in %uth iteration: %s\n",
                        n, i, strerror(-err));
            }
        }
    }
    
    random_delete(random);
    map_clear(m);
    
    srand(time(NULL));
    
    dups2 = 0;
    
    for(i = 0; i < num; ++i) {
        n = rand();
        
        if(map_contains(m, (void *)(long) n)) {
            dups2 += 1;
        } else {
            err = map_insert(m, (void *)(long) n, (void *)(long) n);
            if(err < 0) {
                fprintf(stderr, "Warning: "
                "Map unable to insert %u in %uth iteration: %s\n",
                n, i, strerror(-err));
            }
        }
    }
    
    map_delete(m);
    
    fprintf(stdout,
            "random: After %u iterations there are %u (%lf %%) duplicates\n"
            "rand(): After %u iterations there are %u (%lf %%) duplicates\n", 
            num, dups1, (double) dups1 / num, num, dups2, (double) dups2 / num);
}

void test_range(unsigned int num)
{
    struct random *rand;
    unsigned int i, n;
    
    rand = random_new();
    assert(rand);
    
    for(i = 0; i < num; ++i) {
        n = random_uint_range(rand, 0, 100);
        assert(n <= 100);
        
        n = random_uint_range(rand, 1000, 10000);
        assert(n >= 1000 && n <= 10000);
        
        n = random_uint_range(rand, 99999, 123456);
        assert(n >= 99999 && n <= 123456);
        
        n = random_uint_range(rand, (unsigned int) 1e6 + 1, (unsigned int) 1e8);
        assert(n >= (unsigned int) 1e6 + 1 && n <= (unsigned int) 1e8);
        
        n = random_uint_range(rand, 0, 0);
        assert(n == 0);
        
        n = random_uint_range(rand, 100, 100);
        assert(n == 100);
    }
    
    random_delete(rand);
}

int main(int argc, char *argv[])
{
    unsigned int num;
    
    if(argc == 2)
        num = atoi(argv[1]);
    else
        num = 0;
    
    test_randomness();
    
    if(num) {
        test_seeding(num);
        test_duplicates(num);
        test_range(num);
    }
    
    fprintf(stdout, "Tests finished\n");
    
    return EXIT_SUCCESS;
}