Ejemplo n.º 1
0
    void testSequence(char *seq,
                      size_t len,
                      const char *fmt,
                      Bottle ref,
                      bool testWrite = true)
    {
        char err[1024];
        printf("\n");
        printf("================================================\n");
        printf(" READ %s\n", fmt);
        Bytes b1(seq, len);
        WireTwiddler tt;
        tt.configure(fmt, fmt);
        printf(">>> %s\n", tt.toString().c_str());
        Bottle bot;

        checkTrue(tt.read(bot, b1), "Read failed");
        snprintf(err, 1024, "%s: read %s, expected %s", fmt, bot.toString().c_str(), ref.toString().c_str());
        checkTrue(bot == ref, err);
        printf("[1] %s: read %s as expected\n", fmt, bot.toString().c_str());

        StringInputStream sis;
        sis.add(b1);
        sis.add(b1);
        WireTwiddlerReader twiddled_input(sis, tt);
        Route route;
        bot.clear();
        twiddled_input.reset();
        ConnectionReader::readFromStream(bot, twiddled_input);

        snprintf(err, 1024, "%s: read %s, expected %s", fmt, bot.toString().c_str(), ref.toString().c_str());
        checkTrue(bot == ref, err);
        printf("[2] %s: read %s as expected\n", fmt, bot.toString().c_str());

        bot.clear();
        twiddled_input.reset();
        ConnectionReader::readFromStream(bot, twiddled_input);

        snprintf(err, 1024, "%s: read %s, expected %s", fmt, bot.toString().c_str(), ref.toString().c_str());
        checkTrue(bot == ref, err);
        printf("[3] %s: read %s as expected\n", fmt, bot.toString().c_str());

        if (testWrite) {

            printf("\n");
            printf("================================================\n");
            printf(" WRITE %s\n", fmt);
            ManagedBytes output;
            checkTrue(tt.write(ref, output), "WRITE FAILED");
            snprintf(err, 1024, "WRITE MISMATCH, length %zd, expected %zd", output.length(), len);
            checkTrue(output.length() == len, err);

            for (size_t i = 0; i < output.length(); i++) {
                snprintf(err, 1024, "WRITE MISMATCH, at %zd, have [%d:%c] expected [%d:%c]\n", i, output.get()[i], output.get()[i], seq[i], seq[i]);
                checkTrue(output.get()[i] == seq[i], err);
            }
            printf("[4] %s: wrote %s as expected\n", fmt, bot.toString().c_str());
        }
    }
Ejemplo n.º 2
0
bool testSequence(char *seq, int len, const char *fmt, Bottle ref,
                  bool testWrite = true) {
    printf("\n");
    printf("================================================\n");
    printf(" READ %s\n", fmt);
    Bytes b1(seq,len);
    WireTwiddler tt;
    tt.configure(fmt);
    printf(">>> %s\n", tt.toString().c_str());
    Bottle bot;
    if (!tt.read(bot,b1)) {
        fprintf(stderr,"Read failed\n");
        return 1;
    }
    if (bot!=ref) {
        printf("%s: read %s, expected %s\n", fmt, 
               bot.toString().c_str(),
               ref.toString().c_str());
        printf("MISMATCH\n");
        exit(1);
        return false;
    }
    printf("[1] %s: read %s as expected\n", fmt, bot.toString().c_str());


    StringInputStream sis;
    sis.add(b1);
    sis.add(b1);
    WireTwiddlerReader twiddled_input(sis,tt);
    Route route;
    StreamConnectionReader reader2;
    reader2.reset(twiddled_input,NULL,route,0,false);
    bot.clear();
    twiddled_input.reset();
    bot.read(reader2);

    if (bot!=ref) {
        printf("%s: read %s, expected %s\n", fmt, 
               bot.toString().c_str(),
               ref.toString().c_str());
        printf("MISMATCH\n");
        exit(1);
        return false;
    }
    printf("[2] %s: read %s as expected\n", fmt, bot.toString().c_str());


    StreamConnectionReader reader3;
    reader3.reset(twiddled_input,NULL,route,0,false);
    bot.clear();
    twiddled_input.reset();
    bot.read(reader3);

    if (bot!=ref) {
        printf("%s: read %s, expected %s\n", fmt, 
               bot.toString().c_str(),
               ref.toString().c_str());
        printf("MISMATCH\n");
        exit(1);
        return false;
    }
    printf("[3] %s: read %s as expected\n", fmt, bot.toString().c_str());

    if (testWrite) {
        
        printf("\n");
        printf("================================================\n");
        printf(" WRITE %s\n", fmt);
        ManagedBytes output;
        tt.write(ref,output);
        if (output.length()!=(size_t)len) {
            fprintf(stderr,"WRITE MISMATCH, length %d, expected %d\n",
                    (int)output.length(), len);
            exit(1);
            return false;
        }
        for (size_t i=0; i<output.length(); i++) {
            if (output.get()[i] != seq[i]) {
                fprintf(stderr,"WRITE MISMATCH, at %d, have [%d:%c] expected [%d:%c]\n",
                        (int)i, output.get()[i], output.get()[i], seq[i], seq[i]);
                exit(1);
                return false;
            }
        }
        printf("[4] %s: wrote %s as expected\n", fmt, bot.toString().c_str());
    }

    return true;
}