void wrapInFunction()
{

//! [0]
QByteArray ba("Hello");
//! [0]


//! [1]
QByteArray ba;
ba.resize(5);
ba[0] = 0x3c;
ba[1] = 0xb8;
ba[2] = 0x64;
ba[3] = 0x18;
ba[4] = 0xca;
//! [1]


//! [2]
for (int i = 0; i < ba.size(); ++i) {
    if (ba.at(i) >= 'a' && ba.at(i) <= 'f')
        cout << "Found character in range [a-f]" << endl;
}
//! [2]


//! [3]
QByteArray x("and");
x.prepend("rock ");         // x == "rock and"
x.append(" roll");          // x == "rock and roll"
x.replace(5, 3, "&");       // x == "rock & roll"
//! [3]


//! [4]
QByteArray ba("We must be <b>bold</b>, very <b>bold</b>");
int j = 0;
while ((j = ba.indexOf("<b>", j)) != -1) {
    cout << "Found <b> tag at index position " << j << endl;
    ++j;
}
//! [4]


//! [5]
QByteArray().isNull();          // returns true
QByteArray().isEmpty();         // returns true

QByteArray("").isNull();        // returns false
QByteArray("").isEmpty();       // returns true

QByteArray("abc").isNull();     // returns false
QByteArray("abc").isEmpty();    // returns false
//! [5]


//! [6]
QByteArray ba("Hello");
int n = ba.size();          // n == 5
ba.data()[0];               // returns 'H'
ba.data()[4];               // returns 'o'
ba.data()[5];               // returns '\0'
//! [6]


//! [7]
QByteArray().isEmpty();         // returns true
QByteArray("").isEmpty();       // returns true
QByteArray("abc").isEmpty();    // returns false
//! [7]


//! [8]
QByteArray ba("Hello world");
char *data = ba.data();
while (*data) {
    cout << "[" << *data << "]" << endl;
    ++data;
}
//! [8]


//! [9]
QByteArray ba;
for (int i = 0; i < 10; ++i)
    ba[i] = 'A' + i;
// ba == "ABCDEFGHIJ"
//! [9]


//! [10]
QByteArray ba("Stockholm");
ba.truncate(5);             // ba == "Stock"
//! [10]


//! [11]
QByteArray ba("STARTTLS\r\n");
ba.chop(2);                 // ba == "STARTTLS"
//! [11]


//! [12]
QByteArray x("free");
QByteArray y("dom");
x += y;
// x == "freedom"
//! [12]


//! [13]
QByteArray().isNull();          // returns true
QByteArray("").isNull();        // returns false
QByteArray("abc").isNull();     // returns false
//! [13]


//! [14]
QByteArray ba("Istambul");
ba.fill('o');
// ba == "oooooooo"

ba.fill('X', 2);
// ba == "XX"
//! [14]


//! [15]
QByteArray x("ship");
QByteArray y("air");
x.prepend(y);
// x == "airship"
//! [15]


//! [16]
QByteArray x("free");
QByteArray y("dom");
x.append(y);
// x == "freedom"
//! [16]


//! [17]
QByteArray ba("Meal");
ba.insert(1, QByteArray("ontr"));
// ba == "Montreal"
//! [17]


//! [18]
QByteArray ba("Montreal");
ba.remove(1, 4);
// ba == "Meal"
//! [18]


//! [19]
QByteArray x("Say yes!");
QByteArray y("no");
x.replace(4, 3, y);
// x == "Say no!"
//! [19]


//! [20]
QByteArray ba("colour behaviour flavour neighbour");
ba.replace(QByteArray("ou"), QByteArray("o"));
// ba == "color behavior flavor neighbor"
//! [20]


//! [21]
QByteArray x("sticky question");
QByteArray y("sti");
x.indexOf(y);               // returns 0
x.indexOf(y, 1);            // returns 10
x.indexOf(y, 10);           // returns 10
x.indexOf(y, 11);           // returns -1
//! [21]


//! [22]
QByteArray ba("ABCBA");
ba.indexOf("B");            // returns 1
ba.indexOf("B", 1);         // returns 1
ba.indexOf("B", 2);         // returns 3
ba.indexOf("X");            // returns -1
//! [22]


//! [23]
QByteArray x("crazy azimuths");
QByteArray y("az");
x.lastIndexOf(y);           // returns 6
x.lastIndexOf(y, 6);        // returns 6
x.lastIndexOf(y, 5);        // returns 2
x.lastIndexOf(y, 1);        // returns -1
//! [23]


//! [24]
QByteArray ba("ABCBA");
ba.lastIndexOf("B");        // returns 3
ba.lastIndexOf("B", 3);     // returns 3
ba.lastIndexOf("B", 2);     // returns 1
ba.lastIndexOf("X");        // returns -1
//! [24]


//! [25]
QByteArray url("ftp://ftp.qt-project.org/");
if (url.startsWith("ftp:"))
    ...
//! [25]


//! [26]
QByteArray url("http://qt-project.org/doc/qt-5.0/qtdoc/index.html");
if (url.endsWith(".html"))
    ...
//! [26]


//! [27]
QByteArray x("Pineapple");
QByteArray y = x.left(4);
// y == "Pine"
//! [27]


//! [28]
QByteArray x("Pineapple");
QByteArray y = x.right(5);
// y == "apple"
//! [28]


//! [29]
QByteArray x("Five pineapples");
QByteArray y = x.mid(5, 4);     // y == "pine"
QByteArray z = x.mid(5);        // z == "pineapples"
//! [29]


//! [30]
QByteArray x("Qt by DIGIA");
QByteArray y = x.toLower();
// y == "qt by digia"
//! [30]


//! [31]
QByteArray x("Qt by DIGIA");
QByteArray y = x.toUpper();
// y == "QT BY DIGIA"
//! [31]


//! [32]
QByteArray ba("  lots\t of\nwhitespace\r\n ");
ba = ba.simplified();
// ba == "lots of whitespace";
//! [32]


//! [33]
QByteArray ba("  lots\t of\nwhitespace\r\n ");
ba = ba.trimmed();
// ba == "lots\t of\nwhitespace";
//! [33]


//! [34]
QByteArray x("apple");
QByteArray y = x.leftJustified(8, '.');   // y == "apple..."
//! [34]


//! [35]
QByteArray x("apple");
QByteArray y = x.rightJustified(8, '.');    // y == "...apple"
//! [35]


//! [36]
QByteArray str("FF");
bool ok;
int hex = str.toInt(&ok, 16);     // hex == 255, ok == true
int dec = str.toInt(&ok, 10);     // dec == 0, ok == false
//! [36]


//! [37]
QByteArray str("FF");
bool ok;
long hex = str.toLong(&ok, 16);   // hex == 255, ok == true
long dec = str.toLong(&ok, 10);   // dec == 0, ok == false
//! [37]


//! [38]
QByteArray string("1234.56");
double a = string.toDouble();   // a == 1234.56
//! [38]


//! [39]
QByteArray text("Qt is great!");
text.toBase64();        // returns "UXQgaXMgZ3JlYXQh"
//! [39]

//! [39bis]
QByteArray text("<p>Hello?</p>");
text.toBase64(QByteArray::Base64 | QByteArray::OmitTrailingEquals);      // returns "PHA+SGVsbG8/PC9wPg"
text.toBase64(QByteArray::Base64);                                       // returns "PHA+SGVsbG8/PC9wPg=="
text.toBase64(QByteArray::Base64Url);                                    // returns "PHA-SGVsbG8_PC9wPg=="
text.toBase64(QByteArray::Base64Url | QByteArray::OmitTrailingEquals);   // returns "PHA-SGVsbG8_PC9wPg"
//! [39bis]


//! [40]
QByteArray ba;
int n = 63;
ba.setNum(n);           // ba == "63"
ba.setNum(n, 16);       // ba == "3f"
//! [40]


//! [41]
int n = 63;
QByteArray::number(n);              // returns "63"
QByteArray::number(n, 16);          // returns "3f"
QByteArray::number(n, 16).toUpper();  // returns "3F"
//! [41]


//! [42]
QByteArray ba = QByteArray::number(12.3456, 'E', 3);
// ba == 1.235E+01
//! [42]


//! [43]
 static const char mydata[] = {
    0x00, 0x00, 0x03, 0x84, 0x78, 0x9c, 0x3b, 0x76,
    0xec, 0x18, 0xc3, 0x31, 0x0a, 0xf1, 0xcc, 0x99,
    ...
    0x6d, 0x5b
};

QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
QDataStream in(&data, QIODevice::ReadOnly);
...
//! [43]


//! [44]
QByteArray text = QByteArray::fromBase64("UXQgaXMgZ3JlYXQh");
text.data();            // returns "Qt is great!"
//! [44]

//! [44bis]
QByteArray::fromBase64("PHA+SGVsbG8/PC9wPg==", QByteArray::Base64Encoding); // returns "<p>Hello?</p>"
QByteArray::fromBase64("PHA-SGVsbG8_PC9wPg==", QByteArray::Base64UrlEncoding); // returns "<p>Hello?</p>"
//! [44bis]


//! [45]
QByteArray text = QByteArray::fromHex("517420697320677265617421");
text.data();            // returns "Qt is great!"
//! [45]

//! [46]
QString tmp = "test";
QByteArray text = tmp.toLocal8Bit();
char *data = new char[text.size()];
strcpy(data, text.data());
delete [] data;
//! [46]

//! [47]
QString tmp = "test";
QByteArray text = tmp.toLocal8Bit();
char *data = new char[text.size() + 1];
strcpy(data, text.data());
delete [] data;
//! [47]

//! [48]
QByteArray ba1("ca\0r\0t");
ba1.size();                     // Returns 2.
ba1.constData();                // Returns "ca" with terminating \0.

QByteArray ba2("ca\0r\0t", 3);
ba2.size();                     // Returns 3.
ba2.constData();                // Returns "ca\0" with terminating \0.

QByteArray ba3("ca\0r\0t", 4);
ba3.size();                     // Returns 4.
ba3.constData();                // Returns "ca\0r" with terminating \0.

const char cart[] = {'c', 'a', '\0', 'r', '\0', 't'};
QByteArray ba4(QByteArray::fromRawData(cart, 6));
ba4.size();                     // Returns 6.
ba4.constData();                // Returns "ca\0r\0t" without terminating \0.
//! [48]

}
Esempio n. 2
0
void QPF::addGlyphs(QFontEngine *fe, const QList<CharacterRange> &ranges)
{
    const quint16 glyphCount = fe->glyphCount();

    QByteArray gmap;
    gmap.resize(glyphCount * sizeof(quint32));
    gmap.fill(char(0xff));
    //qDebug() << "glyphCount" << glyphCount;

    QByteArray glyphs;
    if (options & RenderGlyphs) {
        // this is only a rough estimation
        glyphs.reserve(glyphCount 
                * (sizeof(QFontEngineQPF::Glyph) 
                    + qRound(fe->maxCharWidth() * (fe->ascent() + fe->descent()).toReal())));

        QGlyphLayoutArray<10> layout;

        foreach (CharacterRange range, ranges) {
            if (debugVerbosity > 2)
                qDebug() << "rendering range from" << range.start << "to" << range.end;
            for (uint uc = range.start; uc < range.end; ++uc) {
                QChar ch(uc);
                int nglyphs = 10;
                if (!fe->stringToCMap(&ch, 1, &layout, &nglyphs, /*flags*/ 0))
                    continue;

                if (nglyphs != 1)
                    continue;

                const quint32 glyphIndex = layout.glyphs[0];

                if (!glyphIndex)
                    continue;

                Q_ASSERT(glyphIndex < glyphCount);

                QImage img = fe->alphaMapForGlyph(glyphIndex).convertToFormat(QImage::Format_Indexed8);
                glyph_metrics_t metrics = fe->boundingBox(glyphIndex);

                const quint32 oldSize = glyphs.size();
                glyphs.resize(glyphs.size() + sizeof(QFontEngineQPF::Glyph) + img.byteCount());
                uchar *data = reinterpret_cast<uchar *>(glyphs.data() + oldSize);

                uchar *gmapPtr = reinterpret_cast<uchar *>(gmap.data() + glyphIndex * sizeof(quint32));
                qToBigEndian(oldSize, gmapPtr);

                QFontEngineQPF::Glyph *glyph = reinterpret_cast<QFontEngineQPF::Glyph *>(data);
                glyph->width = img.width();
                glyph->height = img.height();
                glyph->bytesPerLine = img.bytesPerLine();
                glyph->x = qRound(metrics.x);
                glyph->y = qRound(metrics.y);
                glyph->advance = qRound(metrics.xoff);
                data += sizeof(QFontEngineQPF::Glyph);

                if (debugVerbosity && uc >= 'A' && uc <= 'z' || debugVerbosity > 1) {
                    qDebug() << "adding glyph with index" << glyphIndex << " uc =" << char(uc) << ":\n"
                        << "    glyph->x =" << glyph->x << "rounded from" << metrics.x << "\n"
                        << "    glyph->y =" << glyph->y << "rounded from" << metrics.y << "\n"
                        << "    width =" << glyph->width << "height =" << glyph->height
                        << "    advance =" << glyph->advance << "rounded from" << metrics.xoff
                        ;
                }

                memcpy(data, img.bits(), img.byteCount());
            }
        }
    }
Esempio n. 3
0
void OctreeTests::propertyFlagsTests() {
    bool verbose = true;
    
    qDebug() << "FIXME: this test is broken and needs to be fixed.";
    qDebug() << "We're disabling this so that ALL_BUILD works";
    return;

    if (verbose) {
        qDebug() << "******************************************************************************************";
    }
    
    qDebug() << "OctreeTests::propertyFlagsTests()";

    {
        if (verbose) {
            qDebug() << "Test 1: EntityProperties: using setHasProperty()";
        }

        EntityPropertyFlags props;
        props.setHasProperty(PROP_VISIBLE);
        props.setHasProperty(PROP_POSITION);
        props.setHasProperty(PROP_RADIUS);
        props.setHasProperty(PROP_MODEL_URL);
        props.setHasProperty(PROP_COMPOUND_SHAPE_URL);
        props.setHasProperty(PROP_ROTATION);
    
        QByteArray encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 13 }));

    }

    {    
        if (verbose) {
            qDebug() << "Test 2: ExamplePropertyFlags: using setHasProperty()";
        }

        EntityPropertyFlags props2;
        props2.setHasProperty(PROP_VISIBLE);
        props2.setHasProperty(PROP_ANIMATION_URL);
        props2.setHasProperty(PROP_ANIMATION_FPS);
        props2.setHasProperty(PROP_ANIMATION_FRAME_INDEX);
        props2.setHasProperty(PROP_ANIMATION_PLAYING);
    
        QByteArray encoded = props2.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 196, 15, 2 }));

        if (verbose) {
            qDebug() << "Test 2b: remove flag with setHasProperty() PROP_PAUSE_SIMULATION";
        }

        encoded = props2.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 136, 30 }));
    }

    {    
        if (verbose) {
            qDebug() << "Test 3: ExamplePropertyFlags: using | operator";
        }
        
        ExamplePropertyFlags props;

        props = ExamplePropertyFlags(EXAMPLE_PROP_VISIBLE) 
                    | ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_URL)
                    | ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_FPS)
                    | ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_FRAME_INDEX)
                    | ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_PLAYING) 
                    | ExamplePropertyFlags(EXAMPLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 196, 15, 2 }));


        if (verbose) {
            qDebug() << "Test 3b: remove flag with -= EXAMPLE_PROP_PAUSE_SIMULATION";
        }
        
        props -= EXAMPLE_PROP_PAUSE_SIMULATION;
    
        encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 136, 30 }));
    }

    {    
        if (verbose) {
            qDebug() << "Test 3c: ExamplePropertyFlags: using |= operator";
        }

        ExamplePropertyFlags props;

        props |= EXAMPLE_PROP_VISIBLE;
        props |= EXAMPLE_PROP_ANIMATION_URL;
        props |= EXAMPLE_PROP_ANIMATION_FPS;
        props |= EXAMPLE_PROP_ANIMATION_FRAME_INDEX;
        props |= EXAMPLE_PROP_ANIMATION_PLAYING;
        props |= EXAMPLE_PROP_PAUSE_SIMULATION;

        QByteArray encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 196, 15, 2 }));
    }

    {    
        if (verbose) {
            qDebug() << "Test 4: ExamplePropertyFlags: using + operator";
        }

        ExamplePropertyFlags props;

        props = ExamplePropertyFlags(EXAMPLE_PROP_VISIBLE) 
                    + ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_URL)
                    + ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_FPS)
                    + ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_FRAME_INDEX)
                    + ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_PLAYING) 
                    + ExamplePropertyFlags(EXAMPLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 196, 15, 2 }));
    }

    {    
        if (verbose) {
            qDebug() << "Test 5: ExamplePropertyFlags: using += operator";
        }
        ExamplePropertyFlags props;

        props += EXAMPLE_PROP_VISIBLE;
        props += EXAMPLE_PROP_ANIMATION_URL;
        props += EXAMPLE_PROP_ANIMATION_FPS;
        props += EXAMPLE_PROP_ANIMATION_FRAME_INDEX;
        props += EXAMPLE_PROP_ANIMATION_PLAYING;
        props += EXAMPLE_PROP_PAUSE_SIMULATION;
    
        QByteArray encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 196, 15, 2 }));
    }

    {    
        if (verbose) {
            qDebug() << "Test 6: ExamplePropertyFlags: using = ... << operator";
        }
        
        ExamplePropertyFlags props;

        props = ExamplePropertyFlags(EXAMPLE_PROP_VISIBLE) 
                    << ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_URL)
                    << ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_FPS)
                    << ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_FRAME_INDEX)
                    << ExamplePropertyFlags(EXAMPLE_PROP_ANIMATION_PLAYING) 
                    << ExamplePropertyFlags(EXAMPLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 196, 15, 2 }));
    }

    {
        if (verbose) {
            qDebug() << "Test 7: ExamplePropertyFlags: using <<= operator";
        }
        
        ExamplePropertyFlags props;

        props <<= EXAMPLE_PROP_VISIBLE;
        props <<= EXAMPLE_PROP_ANIMATION_URL;
        props <<= EXAMPLE_PROP_ANIMATION_FPS;
        props <<= EXAMPLE_PROP_ANIMATION_FRAME_INDEX;
        props <<= EXAMPLE_PROP_ANIMATION_PLAYING;
        props <<= EXAMPLE_PROP_PAUSE_SIMULATION;
    
        QByteArray encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 196, 15, 2 }));
    }

    {
        if (verbose) {
            qDebug() << "Test 8: ExamplePropertyFlags: using << enum operator";
        }
        
        ExamplePropertyFlags props;

        props << EXAMPLE_PROP_VISIBLE;
        props << EXAMPLE_PROP_ANIMATION_URL;
        props << EXAMPLE_PROP_ANIMATION_FPS;
        props << EXAMPLE_PROP_ANIMATION_FRAME_INDEX;
        props << EXAMPLE_PROP_ANIMATION_PLAYING;
        props << EXAMPLE_PROP_PAUSE_SIMULATION;

        QByteArray encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 196, 15, 2 }));
    }

    {
        if (verbose) {
            qDebug() << "Test 9: ExamplePropertyFlags: using << flags operator ";
        }

        ExamplePropertyFlags props;
        ExamplePropertyFlags props2;

        props << EXAMPLE_PROP_VISIBLE;
        props << EXAMPLE_PROP_ANIMATION_URL;
        props << EXAMPLE_PROP_ANIMATION_FPS;

        props2 << EXAMPLE_PROP_ANIMATION_FRAME_INDEX;
        props2 << EXAMPLE_PROP_ANIMATION_PLAYING;
        props2 << EXAMPLE_PROP_PAUSE_SIMULATION;

        props << props2;

        QByteArray encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 196, 15, 2 }));
    }
  
    {
        if (verbose) {
            qDebug() << "Test 10: ExamplePropertyFlags comparison";
        }
        ExamplePropertyFlags propsA;

        if (verbose) {
            qDebug() << "!propsA:" << (!propsA) << "{ expect true }";
        }
        QCOMPARE(!propsA, true);

        propsA << EXAMPLE_PROP_VISIBLE;
        propsA << EXAMPLE_PROP_ANIMATION_URL;
        propsA << EXAMPLE_PROP_ANIMATION_FPS;
        propsA << EXAMPLE_PROP_ANIMATION_FRAME_INDEX;
        propsA << EXAMPLE_PROP_ANIMATION_PLAYING;
        propsA << EXAMPLE_PROP_PAUSE_SIMULATION;

        QCOMPARE(!propsA, false);

        ExamplePropertyFlags propsB;
        propsB << EXAMPLE_PROP_VISIBLE;
        propsB << EXAMPLE_PROP_ANIMATION_URL;
        propsB << EXAMPLE_PROP_ANIMATION_FPS;
        propsB << EXAMPLE_PROP_ANIMATION_FRAME_INDEX;
        propsB << EXAMPLE_PROP_ANIMATION_PLAYING;
        propsB << EXAMPLE_PROP_PAUSE_SIMULATION;

        if (verbose) {
            qDebug() << "propsA == propsB:" << (propsA == propsB) << "{ expect true }";
            qDebug() << "propsA != propsB:" << (propsA != propsB) << "{ expect false }";
        }
        QCOMPARE(propsA == propsB, true);
        QCOMPARE(propsA != propsB, false);
        
        if (verbose) {
            qDebug() << "AFTER propsB -= EXAMPLE_PROP_PAUSE_SIMULATION...";
        }
        
        propsB -= EXAMPLE_PROP_PAUSE_SIMULATION;

        QCOMPARE(propsA == propsB, false);
        if (verbose) {
            qDebug() << "AFTER propsB = propsA...";
        }
        propsB = propsA;
        QCOMPARE(propsA == propsB, true);
    }

    {
        if (verbose) {
            qDebug() << "Test 11: ExamplePropertyFlags testing individual properties";
        }
        ExamplePropertyFlags props;

        if (verbose) {
            qDebug() << "ExamplePropertyFlags props;";
        }
        
        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "Test 11b: props.getHasProperty(EXAMPLE_PROP_VISIBLE)" << (props.getHasProperty(EXAMPLE_PROP_VISIBLE)) 
                        << "{ expect false }";
        }
        QCOMPARE(props.getHasProperty(EXAMPLE_PROP_VISIBLE), false);

        if (verbose) {
            qDebug() << "props << EXAMPLE_PROP_VISIBLE;";
        }
        props << EXAMPLE_PROP_VISIBLE;
        QCOMPARE(props.getHasProperty(EXAMPLE_PROP_VISIBLE), true);

        encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 16 }));
        if (verbose) {
            qDebug() << "props << EXAMPLE_PROP_ANIMATION_URL;";
        }
        props << EXAMPLE_PROP_ANIMATION_URL;
    
        encoded = props.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 136, 16}));
        QCOMPARE(props.getHasProperty(EXAMPLE_PROP_VISIBLE), true);

        if (verbose) {
            qDebug() << "props << ... more ...";
        }
        props << EXAMPLE_PROP_ANIMATION_FPS;
        props << EXAMPLE_PROP_ANIMATION_FRAME_INDEX;
        props << EXAMPLE_PROP_ANIMATION_PLAYING;
        props << EXAMPLE_PROP_PAUSE_SIMULATION;

        encoded = props.encode();
        QCOMPARE(props.getHasProperty(EXAMPLE_PROP_VISIBLE), true);

        if (verbose) {
            qDebug() << "ExamplePropertyFlags propsB = props & EXAMPLE_PROP_VISIBLE;";
        }
        ExamplePropertyFlags propsB = props & EXAMPLE_PROP_VISIBLE;

        QCOMPARE(propsB.getHasProperty(EXAMPLE_PROP_VISIBLE), true);

        encoded = propsB.encode();
        QCOMPARE(encoded, makeQByteArray({ (char) 16 }));

        if (verbose) {
            qDebug() << "ExamplePropertyFlags propsC = ~propsB;";
        }
        ExamplePropertyFlags propsC = ~propsB;
        
        QCOMPARE(propsC.getHasProperty(EXAMPLE_PROP_VISIBLE), false);

        encoded = propsC.encode();
        if (verbose) {
            qDebug() << "propsC... encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }
    }
    
    {
        if (verbose) {
            qDebug() << "Test 12: ExamplePropertyFlags: decode tests";
        }
        ExamplePropertyFlags props;

        props << EXAMPLE_PROP_VISIBLE;
        props << EXAMPLE_PROP_ANIMATION_URL;
        props << EXAMPLE_PROP_ANIMATION_FPS;
        props << EXAMPLE_PROP_ANIMATION_FRAME_INDEX;
        props << EXAMPLE_PROP_ANIMATION_PLAYING;
        props << EXAMPLE_PROP_PAUSE_SIMULATION;

        QByteArray encoded = props.encode();
        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
            qDebug() << "encoded.size()=" << encoded.size();
        }

        ExamplePropertyFlags propsDecoded;
        propsDecoded.decode(encoded);
        
        QCOMPARE(propsDecoded, props);

        QByteArray encodedAfterDecoded = propsDecoded.encode();

        QCOMPARE(encoded, encodedAfterDecoded);

        if (verbose) {
            qDebug() << "fill encoded byte array with extra garbage (as if it was bitstream with more content)";
        }
        QByteArray extraContent;
        extraContent.fill(0xbaU, 10);
        encoded.append(extraContent);

        if (verbose) {
            qDebug() << "encoded.size()=" << encoded.size() << "includes extra garbage";
        }

        ExamplePropertyFlags propsDecodedExtra;
        propsDecodedExtra.decode(encoded);
        
        QCOMPARE(propsDecodedExtra, props);

        QByteArray encodedAfterDecodedExtra = propsDecodedExtra.encode();

        if (verbose) {
            qDebug() << "encodedAfterDecodedExtra=";
            outputBufferBits((const unsigned char*)encodedAfterDecodedExtra.constData(), encodedAfterDecodedExtra.size());
        }
    }
    
    {
        if (verbose) {
            qDebug() << "Test 13: ExamplePropertyFlags: QByteArray << / >> tests";
        }
        ExamplePropertyFlags props;

        props << EXAMPLE_PROP_VISIBLE;
        props << EXAMPLE_PROP_ANIMATION_URL;
        props << EXAMPLE_PROP_ANIMATION_FPS;
        props << EXAMPLE_PROP_ANIMATION_FRAME_INDEX;
        props << EXAMPLE_PROP_ANIMATION_PLAYING;
        props << EXAMPLE_PROP_PAUSE_SIMULATION;

        if (verbose) {
            qDebug() << "testing encoded << props";
        }
        QByteArray encoded;
        encoded << props;

        if (verbose) {
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        ExamplePropertyFlags propsDecoded;
        if (verbose) {
            qDebug() << "testing encoded >> propsDecoded";
        }
        encoded >> propsDecoded;
        
        
        QCOMPARE(propsDecoded, props);
    }

    if (verbose) {
        qDebug() << "******************************************************************************************";
    }
}
Esempio n. 4
0
QByteArray TapParameterDialog::getTreeAsString(st_format_type format)
{
    QByteArray ba;
    QTreeWidgetItemIterator it(ui->statsTreeWidget, QTreeWidgetItemIterator::NotHidden);

    QList<int> col_widths;
    QByteArray footer;

    // Title + header
    switch (format) {
    case ST_FORMAT_PLAIN:
    {
        QTreeWidgetItemIterator width_it(it);
        QString plain_header;
        while (*width_it) {
            QList<QVariant> tid = treeItemData((*width_it));
            int col = 0;
            foreach (QVariant var, tid) {
                if (col_widths.size() <= col) {
                    col_widths.append(ui->statsTreeWidget->headerItem()->text(col).length());
                }
                if (var.type() == QVariant::String) {
                    col_widths[col] = qMax(col_widths[col], itemDataToPlain(var).length());
                }
                col++;
            }
            ++width_it;
        }
        QStringList ph_parts;
        for (int col = 0; col < ui->statsTreeWidget->columnCount() && col < col_widths.length(); col++) {
            ph_parts << ui->statsTreeWidget->headerItem()->text(col);
        }
        plain_header = ph_parts.join(plain_sep_);

        QByteArray top_separator;
        top_separator.fill('=', plain_header.length());
        top_separator.append('\n');
        QString file_header = QString("%1 - %2:\n").arg(windowSubtitle(), cap_file_.fileName());
        footer.fill('-', plain_header.length());
        footer.append('\n');
        plain_header.append('\n');

        ba.append(top_separator);
        ba.append(file_header);
        ba.append(plain_header);
        ba.append(footer);
        break;
    }
    case ST_FORMAT_CSV:
    {
        QString csv_header;
        QStringList ch_parts;
        for (int col = 0; col < ui->statsTreeWidget->columnCount(); col++) {
            ch_parts << QString("\"%1\"").arg(ui->statsTreeWidget->headerItem()->text(col));
        }
        csv_header = ch_parts.join(",");
        csv_header.append('\n');
        ba.append(csv_header.toUtf8().constData());
        break;
    }
    case ST_FORMAT_XML:
    {
        // XXX What's a useful format? This mostly conforms to DocBook.
        ba.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        QString title;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
        title = Qt::escape(windowSubtitle());
#else
        title = QString(windowSubtitle()).toHtmlEscaped();
#endif
        QString xml_header = QString("<table>\n<title>%1</title>\n").arg(title);
        ba.append(xml_header.toUtf8());
        ba.append("<thead>\n<row>\n");
        for (int col = 0; col < ui->statsTreeWidget->columnCount(); col++) {
            title = ui->statsTreeWidget->headerItem()->text(col);
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
            title = Qt::escape(title);
#else
            title = title.toHtmlEscaped();
#endif
            title = QString("  <entry>%1</entry>\n").arg(title);
            ba.append(title.toUtf8());
        }
        ba.append("</row>\n</thead>\n");
        ba.append("<tbody>\n");
        footer = "</tbody>\n</table>\n";
        break;
    }
    case ST_FORMAT_YAML:
    {
        QString yaml_header;
        ba.append("---\n");
        yaml_header = QString("Description: \"%1\"\nFile: \"%2\"\nItems:\n").arg(windowSubtitle()).arg(cap_file_.fileName());
        ba.append(yaml_header.toUtf8());
        break;
    }
    default:
        break;
    }

    // Data
    while (*it) {
        QList<QVariant> tid = treeItemData((*it));
        if (tid.length() < 1) {
            ++it;
            continue;
        }

        if (tid.length() < ui->statsTreeWidget->columnCount()) {
            // Assume we have a header
        }

        // Assume var length == columnCount
        QString line;
        QStringList parts;

        switch (format) {
        case ST_FORMAT_PLAIN:
        {
            int i = 0;
            foreach (QVariant var, tid) {
                parts << itemDataToPlain(var, col_widths[i]);
                i++;
            }
            line = parts.join(plain_sep_);
            line.append('\n');
            break;
        }
        case ST_FORMAT_CSV:
            foreach (QVariant var, tid) {
                if (var.type() == QVariant::String) {
                    parts << QString("\"%1\"").arg(var.toString());
                } else {
                    parts << var.toString();
                }
            }
            line = parts.join(",");
            line.append('\n');
            break;
        case ST_FORMAT_XML:
        {
            line = "<row>\n";
            foreach (QVariant var, tid) {
                QString entry;
    #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
                entry = Qt::escape(var.toString());
    #else
                entry = var.toString().toHtmlEscaped();
    #endif
                line.append(QString("  <entry>%1</entry>\n").arg(entry));
            }
            line.append("</row>\n");
            break;
        }
        case ST_FORMAT_YAML:
        {
            int col = 0;
            QString indent = "-";
            foreach (QVariant var, tid) {
                QString entry;
                if (var.type() == QVariant::String) {
                    entry = QString("\"%1\"").arg(var.toString());
                } else {
                    entry = var.toString();
                }
                line.append(QString("  %1 %2: %3\n").arg(indent).arg(ui->statsTreeWidget->headerItem()->text(col), entry));
                indent = " ";
                col++;
            }
            break;
        }
Esempio n. 5
0
void OctreeTests::propertyFlagsTests() {
    qDebug() << "******************************************************************************************";
    qDebug() << "OctreeTests::propertyFlagsTests()";

    {    
        qDebug() << "Test 1: ModelProperties: using setHasProperty()";
        ModelPropertyFlags props;
        props.setHasProperty(PROP_VISIBLE);
        props.setHasProperty(PROP_POSITION);
        props.setHasProperty(PROP_RADIUS);
        props.setHasProperty(PROP_MODEL_URL);
        props.setHasProperty(PROP_ROTATION);
    
        QByteArray encoded = props.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
    }

    {    
        qDebug() << "Test 2: ParticlePropertyFlags: using setHasProperty()";
        ParticlePropertyFlags props2;
        props2.setHasProperty(PARTICLE_PROP_VISIBLE);
        props2.setHasProperty(PARTICLE_PROP_ANIMATION_URL);
        props2.setHasProperty(PARTICLE_PROP_ANIMATION_FPS);
        props2.setHasProperty(PARTICLE_PROP_ANIMATION_FRAME_INDEX);
        props2.setHasProperty(PARTICLE_PROP_ANIMATION_PLAYING);
        props2.setHasProperty(PARTICLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props2.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());

        qDebug() << "Test 2b: remove flag with setHasProperty() PARTICLE_PROP_PAUSE_SIMULATION";

        props2.setHasProperty(PARTICLE_PROP_PAUSE_SIMULATION, false);
    
        encoded = props2.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());

    }

    {    
        qDebug() << "Test 3: ParticlePropertyFlags: using | operator";
        ParticlePropertyFlags props;

        props = ParticlePropertyFlags(PARTICLE_PROP_VISIBLE) 
                    | ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_URL)
                    | ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FPS)
                    | ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FRAME_INDEX)
                    | ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_PLAYING) 
                    | ParticlePropertyFlags(PARTICLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());

        qDebug() << "Test 3b: remove flag with -= PARTICLE_PROP_PAUSE_SIMULATION";
        props -= PARTICLE_PROP_PAUSE_SIMULATION;
    
        encoded = props.encode();
    
        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
    }

    {    
        qDebug() << "Test 3c: ParticlePropertyFlags: using |= operator";
        ParticlePropertyFlags props;

        props |= PARTICLE_PROP_VISIBLE;
        props |= PARTICLE_PROP_ANIMATION_URL;
        props |= PARTICLE_PROP_ANIMATION_FPS;
        props |= PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props |= PARTICLE_PROP_ANIMATION_PLAYING;
        props |= PARTICLE_PROP_PAUSE_SIMULATION;

        QByteArray encoded = props.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
    }

    {    
        qDebug() << "Test 4: ParticlePropertyFlags: using + operator";
        ParticlePropertyFlags props;

        props = ParticlePropertyFlags(PARTICLE_PROP_VISIBLE) 
                    + ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_URL)
                    + ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FPS)
                    + ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FRAME_INDEX)
                    + ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_PLAYING) 
                    + ParticlePropertyFlags(PARTICLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
    }

    {    
        qDebug() << "Test 4b: ParticlePropertyFlags: using += operator";
        ParticlePropertyFlags props;

        props += PARTICLE_PROP_VISIBLE;
        props += PARTICLE_PROP_ANIMATION_URL;
        props += PARTICLE_PROP_ANIMATION_FPS;
        props += PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props += PARTICLE_PROP_ANIMATION_PLAYING;
        props += PARTICLE_PROP_PAUSE_SIMULATION;
    
        QByteArray encoded = props.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
    }

    {    
        qDebug() << "Test 5: ParticlePropertyFlags: using = ... << operator";
        ParticlePropertyFlags props;

        props = ParticlePropertyFlags(PARTICLE_PROP_VISIBLE) 
                    << ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_URL)
                    << ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FPS)
                    << ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FRAME_INDEX)
                    << ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_PLAYING) 
                    << ParticlePropertyFlags(PARTICLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
    }

    {
        qDebug() << "Test 5b: ParticlePropertyFlags: using <<= operator";
        ParticlePropertyFlags props;

        props <<= PARTICLE_PROP_VISIBLE;
        props <<= PARTICLE_PROP_ANIMATION_URL;
        props <<= PARTICLE_PROP_ANIMATION_FPS;
        props <<= PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props <<= PARTICLE_PROP_ANIMATION_PLAYING;
        props <<= PARTICLE_PROP_PAUSE_SIMULATION;
    
        QByteArray encoded = props.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
    }

    {
        qDebug() << "Test 5c: ParticlePropertyFlags: using << enum operator";
        ParticlePropertyFlags props;

        props << PARTICLE_PROP_VISIBLE;
        props << PARTICLE_PROP_ANIMATION_URL;
        props << PARTICLE_PROP_ANIMATION_FPS;
        props << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props << PARTICLE_PROP_ANIMATION_PLAYING;
        props << PARTICLE_PROP_PAUSE_SIMULATION;

        QByteArray encoded = props.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
    }

    {
        qDebug() << "Test 5d: ParticlePropertyFlags: using << flags operator ";
        ParticlePropertyFlags props;
        ParticlePropertyFlags props2;

        props << PARTICLE_PROP_VISIBLE;
        props << PARTICLE_PROP_ANIMATION_URL;
        props << PARTICLE_PROP_ANIMATION_FPS;

        props2 << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props2 << PARTICLE_PROP_ANIMATION_PLAYING;
        props2 << PARTICLE_PROP_PAUSE_SIMULATION;

        props << props2;

        QByteArray encoded = props.encode();

        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
    }
  
    {
        qDebug() << "Test 6: ParticlePropertyFlags comparison";
        ParticlePropertyFlags propsA;

        qDebug() << "!propsA:" << (!propsA) << "{ expect true }";

        propsA << PARTICLE_PROP_VISIBLE;
        propsA << PARTICLE_PROP_ANIMATION_URL;
        propsA << PARTICLE_PROP_ANIMATION_FPS;
        propsA << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        propsA << PARTICLE_PROP_ANIMATION_PLAYING;
        propsA << PARTICLE_PROP_PAUSE_SIMULATION;

        qDebug() << "!propsA:" << (!propsA) << "{ expect false }";

        ParticlePropertyFlags propsB;
        qDebug() << "!propsB:" << (!propsB) << "{ expect true }";


        propsB << PARTICLE_PROP_VISIBLE;
        propsB << PARTICLE_PROP_ANIMATION_URL;
        propsB << PARTICLE_PROP_ANIMATION_FPS;
        propsB << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        propsB << PARTICLE_PROP_ANIMATION_PLAYING;
        propsB << PARTICLE_PROP_PAUSE_SIMULATION;

        qDebug() << "!propsB:" << (!propsB) << "{ expect false }";

        qDebug() << "propsA == propsB:" << (propsA == propsB) << "{ expect true }";
        qDebug() << "propsA != propsB:" << (propsA != propsB) << "{ expect false }";


        qDebug() << "AFTER propsB -= PARTICLE_PROP_PAUSE_SIMULATION...";
        propsB -= PARTICLE_PROP_PAUSE_SIMULATION;

        qDebug() << "propsA == propsB:" << (propsA == propsB) << "{ expect false }";
        qDebug() << "propsA != propsB:" << (propsA != propsB) << "{ expect true }";

        qDebug() << "AFTER propsB = propsA...";
        propsB = propsA;

        qDebug() << "propsA == propsB:" << (propsA == propsB) << "{ expect true }";
        qDebug() << "propsA != propsB:" << (propsA != propsB) << "{ expect false }";

    }

    {
        qDebug() << "Test 7: ParticlePropertyFlags testing individual properties";
        ParticlePropertyFlags props;

        qDebug() << "ParticlePropertyFlags props;";
        QByteArray encoded = props.encode();
        qDebug() << "props... encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());

        qDebug() << "props.getHasProperty(PARTICLE_PROP_VISIBLE)" << (props.getHasProperty(PARTICLE_PROP_VISIBLE)) 
                        << "{ expect false }";

        qDebug() << "props << PARTICLE_PROP_VISIBLE;";
        props << PARTICLE_PROP_VISIBLE;

        encoded = props.encode();
        qDebug() << "props... encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        qDebug() << "props.getHasProperty(PARTICLE_PROP_VISIBLE)" << (props.getHasProperty(PARTICLE_PROP_VISIBLE)) 
                        << "{ expect true }";

        qDebug() << "props << PARTICLE_PROP_ANIMATION_URL;";
        props << PARTICLE_PROP_ANIMATION_URL;

        encoded = props.encode();
        qDebug() << "props... encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        qDebug() << "props.getHasProperty(PARTICLE_PROP_VISIBLE)" << (props.getHasProperty(PARTICLE_PROP_VISIBLE)) 
                        << "{ expect true }";

        qDebug() << "props << ... more ...";
        props << PARTICLE_PROP_ANIMATION_FPS;
        props << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props << PARTICLE_PROP_ANIMATION_PLAYING;
        props << PARTICLE_PROP_PAUSE_SIMULATION;

        encoded = props.encode();
        qDebug() << "props... encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        qDebug() << "props.getHasProperty(PARTICLE_PROP_VISIBLE)" << (props.getHasProperty(PARTICLE_PROP_VISIBLE)) 
                        << "{ expect true }";

        qDebug() << "ParticlePropertyFlags propsB = props & PARTICLE_PROP_VISIBLE;";
        ParticlePropertyFlags propsB = props & PARTICLE_PROP_VISIBLE;

        qDebug() << "propsB.getHasProperty(PARTICLE_PROP_VISIBLE)" << (propsB.getHasProperty(PARTICLE_PROP_VISIBLE)) 
                        << "{ expect true }";

        encoded = propsB.encode();
        qDebug() << "propsB... encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());

        qDebug() << "ParticlePropertyFlags propsC = ~propsB;";
        ParticlePropertyFlags propsC = ~propsB;
        
        qDebug() << "propsC.getHasProperty(PARTICLE_PROP_VISIBLE)" << (propsC.getHasProperty(PARTICLE_PROP_VISIBLE))
                        << "{ expect false }";

        encoded = propsC.encode();
        qDebug() << "propsC... encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
    }
    
    {
        qDebug() << "Test 8: ParticlePropertyFlags: decode tests";
        ParticlePropertyFlags props;

        props << PARTICLE_PROP_VISIBLE;
        props << PARTICLE_PROP_ANIMATION_URL;
        props << PARTICLE_PROP_ANIMATION_FPS;
        props << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props << PARTICLE_PROP_ANIMATION_PLAYING;
        props << PARTICLE_PROP_PAUSE_SIMULATION;

        QByteArray encoded = props.encode();
        qDebug() << "encoded=";
        outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());

        qDebug() << "encoded.size()=" << encoded.size();

        ParticlePropertyFlags propsDecoded;
        propsDecoded.decode(encoded);
        
        qDebug() << "propsDecoded == props:" << (propsDecoded == props) << "{ expect true }";

        QByteArray encodedAfterDecoded = propsDecoded.encode();

        qDebug() << "encodedAfterDecoded=";
        outputBufferBits((const unsigned char*)encodedAfterDecoded.constData(), encodedAfterDecoded.size());

        qDebug() << "fill encoded byte array with extra garbage (as if it was bitstream with more content)";
        QByteArray extraContent;
        extraContent.fill(0xba, 10);
        encoded.append(extraContent);
        qDebug() << "encoded.size()=" << encoded.size() << "includes extra garbage";

        ParticlePropertyFlags propsDecodedExtra;
        propsDecodedExtra.decode(encoded);
        
        qDebug() << "propsDecodedExtra == props:" << (propsDecodedExtra == props) << "{ expect true }";

        QByteArray encodedAfterDecodedExtra = propsDecodedExtra.encode();

        qDebug() << "encodedAfterDecodedExtra=";
        outputBufferBits((const unsigned char*)encodedAfterDecodedExtra.constData(), encodedAfterDecodedExtra.size());

    }

    qDebug() << "******************************************************************************************";
}
Esempio n. 6
0
void OctreeTests::propertyFlagsTests(bool verbose) {
    int testsTaken = 0;
    int testsPassed = 0;
    int testsFailed = 0;

    if (verbose) {
        qDebug() << "******************************************************************************************";
    }
    
    qDebug() << "OctreeTests::propertyFlagsTests()";

    {
        if (verbose) {
            qDebug() << "Test 1: EntityProperties: using setHasProperty()";
        }
        testsTaken++;

        EntityPropertyFlags props;
        props.setHasProperty(PROP_VISIBLE);
        props.setHasProperty(PROP_POSITION);
        props.setHasProperty(PROP_RADIUS);
        props.setHasProperty(PROP_MODEL_URL);
        props.setHasProperty(PROP_ROTATION);
    
        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }
        
        char expectedBytes[] = { 31 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 1: EntityProperties: using setHasProperty()";
        }

    }

    {    
        if (verbose) {
            qDebug() << "Test 2: ParticlePropertyFlags: using setHasProperty()";
        }
        testsTaken++;

        ParticlePropertyFlags props2;
        props2.setHasProperty(PARTICLE_PROP_VISIBLE);
        props2.setHasProperty(PARTICLE_PROP_ANIMATION_URL);
        props2.setHasProperty(PARTICLE_PROP_ANIMATION_FPS);
        props2.setHasProperty(PARTICLE_PROP_ANIMATION_FRAME_INDEX);
        props2.setHasProperty(PARTICLE_PROP_ANIMATION_PLAYING);
        props2.setHasProperty(PARTICLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props2.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        char expectedBytes[] = { (char)196, (char)15, (char)2 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 2: ParticlePropertyFlags: using setHasProperty()";
        }

        
        if (verbose) {
            qDebug() << "Test 2b: remove flag with setHasProperty() PARTICLE_PROP_PAUSE_SIMULATION";
        }
        testsTaken++;

        props2.setHasProperty(PARTICLE_PROP_PAUSE_SIMULATION, false);
    
        encoded = props2.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        char expectedBytesB[] = { (char)136, (char)30 };
        QByteArray expectedResultB(expectedBytesB, sizeof(expectedBytesB)/sizeof(expectedBytesB[0]));
        
        if (encoded == expectedResultB) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 2b: remove flag with setHasProperty() PARTICLE_PROP_PAUSE_SIMULATION";
        }
    }

    {    
        if (verbose) {
            qDebug() << "Test 3: ParticlePropertyFlags: using | operator";
        }
        testsTaken++;
        
        ParticlePropertyFlags props;

        props = ParticlePropertyFlags(PARTICLE_PROP_VISIBLE) 
                    | ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_URL)
                    | ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FPS)
                    | ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FRAME_INDEX)
                    | ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_PLAYING) 
                    | ParticlePropertyFlags(PARTICLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }
        char expectedBytes[] = { (char)196, (char)15, (char)2 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 3: ParticlePropertyFlags: using | operator";
        }


        if (verbose) {
            qDebug() << "Test 3b: remove flag with -= PARTICLE_PROP_PAUSE_SIMULATION";
        }
        testsTaken++;
        
        props -= PARTICLE_PROP_PAUSE_SIMULATION;
    
        encoded = props.encode();
    
        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }
        
        char expectedBytesB[] = { (char)136, (char)30 };
        QByteArray expectedResultB(expectedBytesB, sizeof(expectedBytesB)/sizeof(expectedBytesB[0]));
        
        if (encoded == expectedResultB) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 3b: remove flag with -= PARTICLE_PROP_PAUSE_SIMULATION";
        }
        
    }

    {    
        if (verbose) {
            qDebug() << "Test 3c: ParticlePropertyFlags: using |= operator";
        }
        testsTaken++;

        ParticlePropertyFlags props;

        props |= PARTICLE_PROP_VISIBLE;
        props |= PARTICLE_PROP_ANIMATION_URL;
        props |= PARTICLE_PROP_ANIMATION_FPS;
        props |= PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props |= PARTICLE_PROP_ANIMATION_PLAYING;
        props |= PARTICLE_PROP_PAUSE_SIMULATION;

        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        char expectedBytes[] = { (char)196, (char)15, (char)2 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - 3c: ParticlePropertyFlags: using |= operator";
        }
    }

    {    
        if (verbose) {
            qDebug() << "Test 4: ParticlePropertyFlags: using + operator";
        }
        testsTaken++;

        ParticlePropertyFlags props;

        props = ParticlePropertyFlags(PARTICLE_PROP_VISIBLE) 
                    + ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_URL)
                    + ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FPS)
                    + ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FRAME_INDEX)
                    + ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_PLAYING) 
                    + ParticlePropertyFlags(PARTICLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        char expectedBytes[] = { (char)196, (char)15, (char)2 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 4: ParticlePropertyFlags: using + operator";
        }
    }

    {    
        if (verbose) {
            qDebug() << "Test 5: ParticlePropertyFlags: using += operator";
        }
        testsTaken++;
        ParticlePropertyFlags props;

        props += PARTICLE_PROP_VISIBLE;
        props += PARTICLE_PROP_ANIMATION_URL;
        props += PARTICLE_PROP_ANIMATION_FPS;
        props += PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props += PARTICLE_PROP_ANIMATION_PLAYING;
        props += PARTICLE_PROP_PAUSE_SIMULATION;
    
        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        char expectedBytes[] = { (char)196, (char)15, (char)2 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 5: ParticlePropertyFlags: using += operator";
        }
    }

    {    
        if (verbose) {
            qDebug() << "Test 6: ParticlePropertyFlags: using = ... << operator";
        }
        testsTaken++;
        
        ParticlePropertyFlags props;

        props = ParticlePropertyFlags(PARTICLE_PROP_VISIBLE) 
                    << ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_URL)
                    << ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FPS)
                    << ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_FRAME_INDEX)
                    << ParticlePropertyFlags(PARTICLE_PROP_ANIMATION_PLAYING) 
                    << ParticlePropertyFlags(PARTICLE_PROP_PAUSE_SIMULATION);
    
        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        char expectedBytes[] = { (char)196, (char)15, (char)2 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 6: ParticlePropertyFlags: using = ... << operator";
        }
    }

    {
        if (verbose) {
            qDebug() << "Test 7: ParticlePropertyFlags: using <<= operator";
        }
        testsTaken++;
        
        ParticlePropertyFlags props;

        props <<= PARTICLE_PROP_VISIBLE;
        props <<= PARTICLE_PROP_ANIMATION_URL;
        props <<= PARTICLE_PROP_ANIMATION_FPS;
        props <<= PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props <<= PARTICLE_PROP_ANIMATION_PLAYING;
        props <<= PARTICLE_PROP_PAUSE_SIMULATION;
    
        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        char expectedBytes[] = { (char)196, (char)15, (char)2 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 7: ParticlePropertyFlags: using <<= operator";
        }
    }

    {
        if (verbose) {
            qDebug() << "Test 8: ParticlePropertyFlags: using << enum operator";
        }
        testsTaken++;
        
        ParticlePropertyFlags props;

        props << PARTICLE_PROP_VISIBLE;
        props << PARTICLE_PROP_ANIMATION_URL;
        props << PARTICLE_PROP_ANIMATION_FPS;
        props << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props << PARTICLE_PROP_ANIMATION_PLAYING;
        props << PARTICLE_PROP_PAUSE_SIMULATION;

        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        char expectedBytes[] = { (char)196, (char)15, (char)2 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 8: ParticlePropertyFlags: using << enum operator";
        }
    }

    {
        if (verbose) {
            qDebug() << "Test 9: ParticlePropertyFlags: using << flags operator ";
        }
        testsTaken++;

        ParticlePropertyFlags props;
        ParticlePropertyFlags props2;

        props << PARTICLE_PROP_VISIBLE;
        props << PARTICLE_PROP_ANIMATION_URL;
        props << PARTICLE_PROP_ANIMATION_FPS;

        props2 << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props2 << PARTICLE_PROP_ANIMATION_PLAYING;
        props2 << PARTICLE_PROP_PAUSE_SIMULATION;

        props << props2;

        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        char expectedBytes[] = { (char)196, (char)15, (char)2 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 9: ParticlePropertyFlags: using << flags operator";
        }
    }
  
    {
        if (verbose) {
            qDebug() << "Test 10: ParticlePropertyFlags comparison";
        }
        ParticlePropertyFlags propsA;

        if (verbose) {
            qDebug() << "!propsA:" << (!propsA) << "{ expect true }";
        }
        testsTaken++;
        bool resultA = (!propsA);
        bool expectedA = true;
        if (resultA == expectedA) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 10a: ParticlePropertyFlags comparison, uninitialized !propsA";
        }

        propsA << PARTICLE_PROP_VISIBLE;
        propsA << PARTICLE_PROP_ANIMATION_URL;
        propsA << PARTICLE_PROP_ANIMATION_FPS;
        propsA << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        propsA << PARTICLE_PROP_ANIMATION_PLAYING;
        propsA << PARTICLE_PROP_PAUSE_SIMULATION;

        if (verbose) {
            qDebug() << "!propsA:" << (!propsA) << "{ expect false }";
        }
        testsTaken++;
        bool resultB = (!propsA);
        bool expectedB = false;
        if (resultB == expectedB) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 10b: ParticlePropertyFlags comparison, initialized !propsA";
        }

        ParticlePropertyFlags propsB;
        propsB << PARTICLE_PROP_VISIBLE;
        propsB << PARTICLE_PROP_ANIMATION_URL;
        propsB << PARTICLE_PROP_ANIMATION_FPS;
        propsB << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        propsB << PARTICLE_PROP_ANIMATION_PLAYING;
        propsB << PARTICLE_PROP_PAUSE_SIMULATION;

        if (verbose) {
            qDebug() << "propsA == propsB:" << (propsA == propsB) << "{ expect true }";
            qDebug() << "propsA != propsB:" << (propsA != propsB) << "{ expect false }";
        }
        testsTaken++;
        bool resultC = (propsA == propsB);
        bool expectedC = true;
        if (resultC == expectedC) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 10c: ParticlePropertyFlags comparison, propsA == propsB";
        }

        testsTaken++;
        bool resultD = (propsA != propsB);
        bool expectedD = false;
        if (resultD == expectedD) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 10d: ParticlePropertyFlags comparison, propsA != propsB";
        }
        
        if (verbose) {
            qDebug() << "AFTER propsB -= PARTICLE_PROP_PAUSE_SIMULATION...";
        }
        
        propsB -= PARTICLE_PROP_PAUSE_SIMULATION;

        if (verbose) {
            qDebug() << "propsA == propsB:" << (propsA == propsB) << "{ expect false }";
            qDebug() << "propsA != propsB:" << (propsA != propsB) << "{ expect true }";
        }
        testsTaken++;
        bool resultE = (propsA == propsB);
        bool expectedE = false;
        if (resultE == expectedE) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 10e: ParticlePropertyFlags comparison, AFTER propsB -= PARTICLE_PROP_PAUSE_SIMULATION";
        }
        
        if (verbose) {
            qDebug() << "AFTER propsB = propsA...";
        }
        propsB = propsA;
        if (verbose) {
            qDebug() << "propsA == propsB:" << (propsA == propsB) << "{ expect true }";
            qDebug() << "propsA != propsB:" << (propsA != propsB) << "{ expect false }";
        }
        testsTaken++;
        bool resultF = (propsA == propsB);
        bool expectedF = true;
        if (resultF == expectedF) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 10f: ParticlePropertyFlags comparison, AFTER propsB = propsA";
        }
    }

    {
        if (verbose) {
            qDebug() << "Test 11: ParticlePropertyFlags testing individual properties";
        }
        ParticlePropertyFlags props;

        if (verbose) {
            qDebug() << "ParticlePropertyFlags props;";
        }
        
        QByteArray encoded = props.encode();

        if (verbose) {
            qDebug() << "props... encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        char expectedBytes[] = { 0 };
        QByteArray expectedResult(expectedBytes, sizeof(expectedBytes)/sizeof(expectedBytes[0]));
        
        testsTaken++;
        if (encoded == expectedResult) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 11a: ParticlePropertyFlags testing individual properties";
        }


        if (verbose) {
            qDebug() << "Test 11b: props.getHasProperty(PARTICLE_PROP_VISIBLE)" << (props.getHasProperty(PARTICLE_PROP_VISIBLE)) 
                        << "{ expect false }";
        }
        testsTaken++;
        bool resultB = props.getHasProperty(PARTICLE_PROP_VISIBLE);
        bool expectedB = false;
        if (resultB == expectedB) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 11b: props.getHasProperty(PARTICLE_PROP_VISIBLE)";
        }

        if (verbose) {
            qDebug() << "props << PARTICLE_PROP_VISIBLE;";
        }
        props << PARTICLE_PROP_VISIBLE;
        testsTaken++;
        bool resultC = props.getHasProperty(PARTICLE_PROP_VISIBLE);
        bool expectedC = true;
        if (resultC == expectedC) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 11c: props.getHasProperty(PARTICLE_PROP_VISIBLE) after props << PARTICLE_PROP_VISIBLE";
        }

        encoded = props.encode();

        if (verbose) {
            qDebug() << "props... encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
            qDebug() << "props.getHasProperty(PARTICLE_PROP_VISIBLE)" << (props.getHasProperty(PARTICLE_PROP_VISIBLE)) 
                            << "{ expect true }";
        }

        char expectedBytesC[] = { 16 };
        QByteArray expectedResultC(expectedBytesC, sizeof(expectedBytesC)/sizeof(expectedBytesC[0]));
        
        testsTaken++;
        if (encoded == expectedResultC) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 11c: ParticlePropertyFlags testing individual properties";
        }

        if (verbose) {
            qDebug() << "props << PARTICLE_PROP_ANIMATION_URL;";
        }
        props << PARTICLE_PROP_ANIMATION_URL;

        encoded = props.encode();
        if (verbose) {
            qDebug() << "props... encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
            qDebug() << "props.getHasProperty(PARTICLE_PROP_VISIBLE)" << (props.getHasProperty(PARTICLE_PROP_VISIBLE)) 
                            << "{ expect true }";
        }
        char expectedBytesD[] = { (char)136, (char)16 };
        QByteArray expectedResultD(expectedBytesD, sizeof(expectedBytesD)/sizeof(expectedBytesD[0]));
        
        testsTaken++;
        if (encoded == expectedResultD) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 11d: ParticlePropertyFlags testing individual properties";
        }
        testsTaken++;
        bool resultE = props.getHasProperty(PARTICLE_PROP_VISIBLE);
        bool expectedE = true;
        if (resultE == expectedE) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 11e: props.getHasProperty(PARTICLE_PROP_VISIBLE) after props << PARTICLE_PROP_ANIMATION_URL";
        }


        if (verbose) {
            qDebug() << "props << ... more ...";
        }
        props << PARTICLE_PROP_ANIMATION_FPS;
        props << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props << PARTICLE_PROP_ANIMATION_PLAYING;
        props << PARTICLE_PROP_PAUSE_SIMULATION;

        encoded = props.encode();
        if (verbose) {
            qDebug() << "props... encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
            qDebug() << "props.getHasProperty(PARTICLE_PROP_VISIBLE)" << (props.getHasProperty(PARTICLE_PROP_VISIBLE)) 
                            << "{ expect true }";
        }
        testsTaken++;
        bool resultF = props.getHasProperty(PARTICLE_PROP_VISIBLE);
        bool expectedF = true;
        if (resultF == expectedF) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 11f: props.getHasProperty(PARTICLE_PROP_VISIBLE) after props << more";
        }

        if (verbose) {
            qDebug() << "ParticlePropertyFlags propsB = props & PARTICLE_PROP_VISIBLE;";
        }
        ParticlePropertyFlags propsB = props & PARTICLE_PROP_VISIBLE;

        if (verbose) {
            qDebug() << "propsB.getHasProperty(PARTICLE_PROP_VISIBLE)" << (propsB.getHasProperty(PARTICLE_PROP_VISIBLE)) 
                        << "{ expect true }";
        }
        testsTaken++;
        bool resultG = propsB.getHasProperty(PARTICLE_PROP_VISIBLE);
        bool expectedG = true;
        if (resultG == expectedG) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 11g: propsB = props & PARTICLE_PROP_VISIBLE";
        }

        encoded = propsB.encode();
        if (verbose) {
            qDebug() << "propsB... encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }
        char expectedBytesH[] = { 16 };
        QByteArray expectedResultH(expectedBytesC, sizeof(expectedBytesH)/sizeof(expectedBytesH[0]));
        
        testsTaken++;
        if (encoded == expectedResultH) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 11h: ParticlePropertyFlags testing individual properties";
        }

        if (verbose) {
            qDebug() << "ParticlePropertyFlags propsC = ~propsB;";
        }
        ParticlePropertyFlags propsC = ~propsB;
        
        if (verbose) {
            qDebug() << "propsC.getHasProperty(PARTICLE_PROP_VISIBLE)" << (propsC.getHasProperty(PARTICLE_PROP_VISIBLE))
                        << "{ expect false }";
        }
        testsTaken++;
        bool resultI = propsC.getHasProperty(PARTICLE_PROP_VISIBLE);
        bool expectedI = false;
        if (resultI == expectedI) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 11i: propsC = ~propsB";
        }

        encoded = propsC.encode();
        if (verbose) {
            qDebug() << "propsC... encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }
    }
    
    {
        if (verbose) {
            qDebug() << "Test 12: ParticlePropertyFlags: decode tests";
        }
        ParticlePropertyFlags props;

        props << PARTICLE_PROP_VISIBLE;
        props << PARTICLE_PROP_ANIMATION_URL;
        props << PARTICLE_PROP_ANIMATION_FPS;
        props << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props << PARTICLE_PROP_ANIMATION_PLAYING;
        props << PARTICLE_PROP_PAUSE_SIMULATION;

        QByteArray encoded = props.encode();
        if (verbose) {
            qDebug() << "encoded=";
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
            qDebug() << "encoded.size()=" << encoded.size();
        }

        ParticlePropertyFlags propsDecoded;
        propsDecoded.decode(encoded);
        
        if (verbose) {
            qDebug() << "propsDecoded == props:" << (propsDecoded == props) << "{ expect true }";
        }
        testsTaken++;
        bool resultA = (propsDecoded == props);
        bool expectedA = true;
        if (resultA == expectedA) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 12a: propsDecoded == props";
        }

        QByteArray encodedAfterDecoded = propsDecoded.encode();

        if (verbose) {
            qDebug() << "encodedAfterDecoded=";
            outputBufferBits((const unsigned char*)encodedAfterDecoded.constData(), encodedAfterDecoded.size());
        }
        testsTaken++;
        bool resultB = (encoded == encodedAfterDecoded);
        bool expectedB = true;
        if (resultB == expectedB) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 12b: (encoded == encodedAfterDecoded)";
        }

        if (verbose) {
            qDebug() << "fill encoded byte array with extra garbage (as if it was bitstream with more content)";
        }
        QByteArray extraContent;
        extraContent.fill(0xba, 10);
        encoded.append(extraContent);

        if (verbose) {
            qDebug() << "encoded.size()=" << encoded.size() << "includes extra garbage";
        }

        ParticlePropertyFlags propsDecodedExtra;
        propsDecodedExtra.decode(encoded);
        
        if (verbose) {
            qDebug() << "propsDecodedExtra == props:" << (propsDecodedExtra == props) << "{ expect true }";
        }
        testsTaken++;
        bool resultC = (propsDecodedExtra == props);
        bool expectedC = true;
        if (resultC == expectedC) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 12c: (propsDecodedExtra == props)";
        }

        QByteArray encodedAfterDecodedExtra = propsDecodedExtra.encode();

        if (verbose) {
            qDebug() << "encodedAfterDecodedExtra=";
            outputBufferBits((const unsigned char*)encodedAfterDecodedExtra.constData(), encodedAfterDecodedExtra.size());
        }
    }
    
    {
        if (verbose) {
            qDebug() << "Test 13: ParticlePropertyFlags: QByteArray << / >> tests";
        }
        ParticlePropertyFlags props;

        props << PARTICLE_PROP_VISIBLE;
        props << PARTICLE_PROP_ANIMATION_URL;
        props << PARTICLE_PROP_ANIMATION_FPS;
        props << PARTICLE_PROP_ANIMATION_FRAME_INDEX;
        props << PARTICLE_PROP_ANIMATION_PLAYING;
        props << PARTICLE_PROP_PAUSE_SIMULATION;

        if (verbose) {
            qDebug() << "testing encoded << props";
        }
        QByteArray encoded;
        encoded << props;

        if (verbose) {
            outputBufferBits((const unsigned char*)encoded.constData(), encoded.size());
        }

        ParticlePropertyFlags propsDecoded;
        if (verbose) {
            qDebug() << "testing encoded >> propsDecoded";
        }
        encoded >> propsDecoded;

        if (verbose) {
            qDebug() << "propsDecoded==props" << (propsDecoded==props);
        }

        testsTaken++;
        bool resultA = (propsDecoded == props);
        bool expectedA = true;
        if (resultA == expectedA) {
            testsPassed++;
        } else {
            testsFailed++;
            qDebug() << "FAILED - Test 13: ParticlePropertyFlags: QByteArray << / >> tests";
        }
    }

    qDebug() << "   tests passed:" << testsPassed << "out of" << testsTaken;
    if (verbose) {
        qDebug() << "******************************************************************************************";
    }
}
Esempio n. 7
0
void AudioThr::run()
{
	setPriority( QThread::HighestPriority );

	QMutex emptyBufferMutex;
	bool paused = false;
	tmp_br = tmp_time = 0;
#ifdef Q_OS_WIN
	canRefresh = false;
#endif
	while ( !br )
	{
		Packet packet;
		double delay = 0.0, audio_pts = 0.0; //"audio_pts" odporny na zerowanie przy przewijaniu
		Decoder *last_dec = dec;
		int bytes_consumed = -1;
		while ( !br && dec == last_dec )
		{
			playC.aPackets.lock();
			const bool hasAPackets = playC.aPackets.packetCount();
			bool hasBufferedSamples = false;
			if ( playC.endOfStream && !hasAPackets )
				foreach ( AudioFilter *filter, filters )
					if ( filter->bufferedSamples() )
					{
						hasBufferedSamples = true;
						break;
					}
			if ( playC.paused || ( !hasAPackets && !hasBufferedSamples ) || playC.waitForData )
			{
#ifdef Q_OS_WIN
				canRefresh = false;
#endif
				tmp_br = tmp_time = 0;
				if ( playC.paused && !paused )
				{
					doSilence = -1.0;
					writer->pause();
					paused = true;
					emit pauseVisSig( paused );
				}
				playC.aPackets.unlock();

				if ( !playC.paused )
					waiting = playC.fullBufferB = true;

				playC.emptyBufferCond.wait( &emptyBufferMutex, MUTEXWAIT_TIMEOUT );
				emptyBufferMutex.unlock();
				continue;
			}
			if ( paused )
			{
				paused = false;
				emit pauseVisSig( paused );
			}
			waiting = false;

			const bool flushAudio = playC.flushAudio;

			if ( !hasBufferedSamples && ( bytes_consumed < 0 || flushAudio ) )
				packet = playC.aPackets.dequeue();
			else if ( hasBufferedSamples )
				packet.ts = audio_pts + playC.audio_last_delay + delay; //szacowanie czasu
			playC.aPackets.unlock();
			playC.fullBufferB = true;

			mutex.lock();
			if ( br )
			{
				mutex.unlock();
				break;
			}

			QByteArray decoded;
			if ( !hasBufferedSamples )
			{
				bytes_consumed = dec->decode( packet, decoded, flushAudio );
				tmp_br += bytes_consumed;
			}

#ifndef Q_OS_WIN
			if ( tmp_time >= 1000.0 )
			{
				emit playC.updateBitrate( round( ( tmp_br << 3 ) / tmp_time ), -1, -1.0 );
				tmp_br = tmp_time = 0;
			}
#endif

			delay = writer->getParam( "delay" ).toDouble();
			foreach ( AudioFilter *filter, filters )
			{
				if ( flushAudio )
					filter->clearBuffers();
				delay += filter->filter( decoded, hasBufferedSamples );
			}

			if ( flushAudio )
				playC.flushAudio = false;

			int decodedSize = decoded.size();
			int decodedPos = 0;
			br2 = false;
			while ( decodedSize > 0 && !playC.paused && !br && !br2 )
			{
				const double max_len = 0.02; //TODO: zrobić opcje?
				const int chunk = qMin( decodedSize, ( int )( ceil( realSample_rate * max_len ) * realChannels * sizeof( float ) ) );
				const float vol = ( playC.muted || playC.vol == 0.0 ) ? 0.0 : playC.replayGain * ( playC.vol == 1.0 ? 1.0 : playC.vol * playC.vol );

				QByteArray decodedChunk;
				if ( vol == 0.0 )
					decodedChunk.fill( 0, chunk );
				else
					decodedChunk = QByteArray::fromRawData( decoded.data() + decodedPos, chunk );

				decodedPos += chunk;
				decodedSize -= chunk;

				playC.audio_last_delay = ( double )decodedChunk.size() / ( double )( sizeof( float ) * realChannels * realSample_rate );
				if ( packet.ts.isValid() )
				{
					audio_pts = playC.audio_current_pts = packet.ts - delay;
					if ( !playC.vThr )
					{
#ifdef Q_OS_WIN
						playC.chPos( playC.audio_current_pts, playC.flushAudio );
#else
						playC.chPos( playC.audio_current_pts );
#endif
					}
				}

				tmp_time += playC.audio_last_delay * 1000.0;
				packet.ts += playC.audio_last_delay;

#ifdef Q_OS_WIN
				canRefresh = true;
#endif

				if ( playC.skipAudioFrame <= 0.0 )
				{
					const double speed = playC.speed;
					if ( speed != lastSpeed )
					{
						resampler_create();
						lastSpeed = speed;
					}

					if ( vol != 1.0 && vol > 0.0 )
					{
						const int size = decodedChunk.size() / sizeof( float );
						float *data = ( float * )decodedChunk.data();
						for ( int i = 0 ; i < size ; ++i )
							data[ i ] *= vol;
					}

					foreach ( QMPlay2Extensions *vis, visualizations )
						vis->sendSoundData( decodedChunk );

					QByteArray dataToWrite;
					if ( sndResampler.isOpen() )
						sndResampler.convert( decodedChunk, dataToWrite );
					else
						dataToWrite = decodedChunk;

					if ( doSilence >= 0.0 && vol > 0.0 )
					{
						silenceChMutex.lock();
						if ( doSilence >= 0.0 )
						{
							float *data = ( float * )dataToWrite.data();
							const int s = dataToWrite.size() / sizeof( float );
							for ( int i = 0 ; i < s ; i += channels )
							{
								for ( int j = 0 ; j < channels ; ++j )
									data[ i+j ] *= doSilence;
								doSilence -= silence_step;
								if ( doSilence < 0.0 )
									doSilence = 0.0;
								else if ( doSilence > 1.0 )
								{
									doSilence = -1.0;
									break;
								}
							}
						}
						silenceChMutex.unlock();
					}

					writer->write( dataToWrite );
				}
				else
					playC.skipAudioFrame -= playC.audio_last_delay;
			}

			mutex.unlock();

			if ( playC.flushAudio || packet.data.size() == bytes_consumed || ( !bytes_consumed && !decoded.size() ) )
			{
				bytes_consumed = -1;
				packet = Packet();
			}
			else if ( bytes_consumed != packet.data.size() )
				packet.data.remove( 0, bytes_consumed );
		}
	}
}
Esempio n. 8
0
//from Kwalletbackend.
// this should be SHA-512 for release probably
QString Hash::password2hash(const QByteArray& password) {
  SHA1 sha;
  QByteArray hash;
  hash.resize(20);
  hash.fill(0);
  int shasz = sha.size() / 8;

  assert(shasz >= 20);

  QByteArray block1(shasz, 0);

  sha.process(password.data(), qMin(password.size(), 16));

  // To make brute force take longer
  for (int i = 0; i < 2000; i++) {
    memcpy(block1.data(), sha.hash(), shasz);
    sha.reset();
    sha.process(block1.data(), shasz);
  }

  sha.reset();

  if (password.size() > 16) {
    sha.process(password.data() + 16, qMin(password.size() - 16, 16));
    QByteArray block2(shasz, 0);
    // To make brute force take longer
    for (int i = 0; i < 2000; i++) {
      memcpy(block2.data(), sha.hash(), shasz);
      sha.reset();
      sha.process(block2.data(), shasz);
    }

    sha.reset();

    if (password.size() > 32) {
      sha.process(password.data() + 32, qMin(password.size() - 32, 16));

      QByteArray block3(shasz, 0);
      // To make brute force take longer
      for (int i = 0; i < 2000; i++) {
        memcpy(block3.data(), sha.hash(), shasz);
        sha.reset();
        sha.process(block3.data(), shasz);
      }

      sha.reset();

      if (password.size() > 48) {
        sha.process(password.data() + 48, password.size() - 48);

        QByteArray block4(shasz, 0);
        // To make brute force take longer
        for (int i = 0; i < 2000; i++) {
          memcpy(block4.data(), sha.hash(), shasz);
          sha.reset();
          sha.process(block4.data(), shasz);
        }

        sha.reset();
        // split 14/14/14/14
        hash.resize(56);
        memcpy(hash.data(),      block1.data(), 14);
        memcpy(hash.data() + 14, block2.data(), 14);
        memcpy(hash.data() + 28, block3.data(), 14);
        memcpy(hash.data() + 42, block4.data(), 14);
        block4.fill(0);
      } else {
        // split 20/20/16
        hash.resize(56);
        memcpy(hash.data(),      block1.data(), 20);
        memcpy(hash.data() + 20, block2.data(), 20);
        memcpy(hash.data() + 40, block3.data(), 16);
      }
      block3.fill(0);
    } else {
      // split 20/20
      hash.resize(40);
      memcpy(hash.data(),      block1.data(), 20);
      memcpy(hash.data() + 20, block2.data(), 20);
    }
    block2.fill(0);
  } else {
    // entirely block1
    hash.resize(20);
    memcpy(hash.data(), block1.data(), 20);
  }

  block1.fill(0);
  //MCH: to store hash as a String...
  QString output, tmp;
  unsigned char *digest;
  digest = (unsigned char*) hash.data();
  for (int i = 0; i < 20; ++i)
    output += tmp.sprintf("%02x", digest[i]);
  output = output.toUpper();

  qDebug()<<"HASH::Result  == "<<output;

  return output;
}
bool MotorolaSRecordProvider::loadFromMotFile(QString name, QByteArray & data,MotorolaSRecordHeader & h)
{
    Q_UNUSED(h);
    int lastAddress = 0;
    unsigned int currentAddress;
    int bytesCount = 0;
    unsigned char crc;
    data.clear();


    QFile motFile(name);
    if(!motFile.open(QIODevice::ReadOnly))return false;
    while(!motFile.atEnd())
    {
        QByteArray srecord = motFile.readLine();
        if(srecord[srecord.size()-1]=='\n') srecord=srecord.left(srecord.size()-1);
        if(srecord[srecord.size()-1]=='\r') srecord=srecord.left(srecord.size()-1);

        srecord[0] = '0';

        QByteArray record;
        record=  QByteArray::fromHex(srecord);
        QByteArray ca;

        crc = 0;

        for(int i=0;i<record.size()-1;i++)
        {
            crc+=record[i];
        }

        crc=0x100-crc+1;

//        if(crc!=(unsigned char)record[record.size()-1]) return false; // invalid crc

        bytesCount = record[1];

        if((int)record[0]==0) // header record
        {
            // do nothing. no examples found
            continue;
        }

        if((int)record[0]==1) // 2 address bytes
        {
            ca = record.left(4).right(2);
            unsigned char a = ca.at(0);
            unsigned char b = ca.at(1);
            currentAddress = a*0x100+b;

            bytesCount=bytesCount-1-2;
        }

        if((int)record[0]==2) // 3 address bytes
        {
            ca = record.left(5).right(3);
            unsigned char a = ca.at(0);
            unsigned char b = ca.at(1);
            unsigned char c = ca.at(2);
            currentAddress = a*0x10000+b*0x100+c;
            bytesCount=bytesCount-1-3;
        }

        if((int)record[0]==3) // 4 address bytes
        {
            ca = record.left(6).right(4);
            unsigned char a = ca.at(0);
            unsigned char b = ca.at(1);
            unsigned char c = ca.at(2);
            unsigned char d = ca.at(3);
            currentAddress = a*0x1000000+b*0x10000+c*0x100+d;

            bytesCount=bytesCount-1-4;
        }

        //other type record not containing data

        if((int)record[0]==4 || (int)record[0]==5 || (int)record[0]==6 || (int)record[0]==7 || (int)record[0]==8 || (int)record[0]==9)
        {
            continue;
        }


        record = record.left(record.length()-1).right(bytesCount);


        if(currentAddress +record.length() > abs(data.length()))
        {
            QByteArray br;
            br.fill(0x00,currentAddress+record.length());

            data.append(br);
        }

        data.replace(currentAddress,record.length(),record);

        lastAddress = currentAddress + record.length();

}

    data = data.left(lastAddress);
return true;
}
Esempio n. 10
0
void udpthread::run()
{
    QByteArray temp;
    temp.resize(4);
    temp.fill(0x00, 4);
    int i = 0;
    unsigned short broad = 0;
    unsigned short channel = 0;
    unsigned short parameter = 0;
    unsigned int index = 0;
    unsigned short cmd = 0;
    while(flag) {
        /* 设备信息 */
        temp[1] = 0x10;
        temp[3] = 0x11;
        i = recv_data.indexOf(temp);
        if (i > 0 && checkdata(recv_data.mid(i - 2, 136))) {
            broad = (recv_data[i - 2] << 8) | recv_data[i - 1];
            emit broadinfo(broad, recv_data.mid(i + 4, 128));
            recv_data.remove(0, i + 134);
        }
        /* 配置信息 */
        temp[1] = 0x11;
        temp[3] = 0x00;
        i = recv_data.indexOf(temp);
        if (i > 0 && checkdata(recv_data.mid(i - 2, 8))) {
            broad = (recv_data[i - 2] << 8) | recv_data[i - 1];
            emit configinfo(broad);
            recv_data.remove(0, i + 6);
        }
        /* adc通道实时信息 */
        temp[1] = 0x14;
        temp[3] = 0x00;
        i = recv_data.indexOf(temp.mid(0, 2));
        if (i > 0 && checkdata(recv_data.mid(i - 2, 525))) {
            broad = (recv_data[i - 2] << 8) | recv_data[i - 1];
            channel = (recv_data[i + 2] << 8) | recv_data[i + 3];
            index = (recv_data[i + 4] << 24) | (recv_data[i + 5] << 16) |
                    (recv_data[i + 6] << 8) | recv_data[i + 7];
            emit adcdata(broad, channel, index, recv_data.mid(i + 8, 512));
            recv_data.remove(0, i + 523);
        }
        /* sd卡信息 */
        temp[1] = 0x13;
        temp[3] = 0x00;
        i = recv_data.indexOf(temp.mid(0, 2));
        if (i > 0 && checkdata(recv_data.mid(i - 2, 525))) {
            broad = (recv_data[i - 2] << 8) | recv_data[i - 1];
            parameter = (recv_data[i + 2] << 8) | recv_data[i + 3];
            index = (recv_data[i + 4] << 24) | (recv_data[i + 5] << 16) |
                    (recv_data[i + 6] << 8) | recv_data[i + 7];
            emit sddata(broad, parameter, index, recv_data.mid(i + 8, 512));
            recv_data.remove(0, i + 523);
        }
        /* 固件下载起始应答 */
        temp[1] = 0x20;
        temp[3] = 0x00;
        i = recv_data.indexOf(temp);
        if (i > 0 && checkdata(recv_data.mid(i - 2, 18))) {
            broad = (recv_data[i - 2] << 8) | recv_data[i - 1];
            emit fireware_write_start_ack(broad);
            recv_data.remove(0, i + 16);
        }
        /* 固件下载请求下一个包 */
        temp[1] = 0x21;
        temp[3] = 0x00;
        i = recv_data.indexOf(temp);
        if (i > 0 && checkdata(recv_data.mid(i - 2, 15))) {
            broad = (recv_data[i - 2] << 8) | recv_data[i - 1];
            index = (recv_data[i + 4] << 40) | (recv_data[i + 5] << 32) |
                    (recv_data[i + 6] << 24) | (recv_data[i + 7] << 16) |
                    (recv_data[i + 8] << 8) | (recv_data[i + 9]);
            emit fireware_next_package_request(broad, index);
            recv_data.remove(0, i + 13);
        }
        /* 固件下载重新请求包 */
        temp[1] = 0x22;
        temp[3] = 0x00;
        i = recv_data.indexOf(temp);
        if (i > 0 && checkdata(recv_data.mid(i - 2, 15))) {
            broad = (recv_data[i - 2] << 8) | recv_data[i - 1];
            index = (recv_data[i + 4] << 40) | (recv_data[i + 5] << 32) |
                    (recv_data[i + 6] << 24) | (recv_data[i + 7] << 16) |
                    (recv_data[i + 8] << 8) | (recv_data[i + 9]);
            emit fireware_repeat_package_request(broad, index);
            recv_data.remove(0, i + 13);
        }
        /* 错误信息 */
        temp[1] = 0x50;
        temp[3] = 0x00;
        i = recv_data.indexOf(temp.mid(0, 2));
        if (i > 0 && checkdata(recv_data.mid(i - 2, 8))) {
            broad = (recv_data[i - 2] << 8) | recv_data[i - 1];
            parameter = (recv_data[i + 2] << 8) | recv_data[i + 3];
            emit error(broad, cmd);
            recv_data.remove(0, i + 6);
        }
    }
    quit();
}