summaryrefslogtreecommitdiffstats
path: root/tests/manual/wasm/qstdweb/iodevices_main.cpp
blob: 0dbdd0084e871e168288128e61a62a0d00f5aa2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtCore/QtCore>
#include <QtCore/private/qstdweb_p.h>

#include <qtwasmtestlib.h>

#include "emscripten.h"

using qstdweb::ArrayBuffer;
using qstdweb::Uint8Array;
using qstdweb::Blob;
using qstdweb::BlobIODevice;
using qstdweb::Uint8ArrayIODevice;

class WasmIoDevicesTest: public QObject
{
    Q_OBJECT

private slots:
    void blobIODevice();
    void uint8ArrayIODevice();
};

// Creates a test arraybuffer with byte values [0..size] % 256 * 2
char testByteValue(int i) { return (i % 256) * 2; }
ArrayBuffer createTestArrayBuffer(int size)
{
    ArrayBuffer buffer(size);
    Uint8Array array(buffer);
    for (int i = 0; i < size; ++i)
        array.val().set(i, testByteValue(i));
    return buffer;
}

void WasmIoDevicesTest::blobIODevice()
{
    if (!qstdweb::canBlockCallingThread()) {
        QtWasmTest::completeTestFunction(QtWasmTest::TestResult::Skip, "requires asyncify");
        return;
    }

    // Create test buffer and BlobIODevice
    const int bufferSize = 16;
    BlobIODevice blobDevice(Blob::fromArrayBuffer(createTestArrayBuffer(bufferSize)));

    // Read back byte for byte from the device
    QWASMVERIFY(blobDevice.open(QIODevice::ReadOnly));
    for (int i = 0; i < bufferSize; ++i) {
        char byte;
        blobDevice.seek(i);
        blobDevice.read(&byte, 1);
        QWASMCOMPARE(byte, testByteValue(i));
    }

    blobDevice.close();
    QWASMVERIFY(!blobDevice.open(QIODevice::WriteOnly));
    QWASMSUCCESS();
}

void WasmIoDevicesTest::uint8ArrayIODevice()
{
    // Create test buffer and Uint8ArrayIODevice
    const int bufferSize = 1024;
    Uint8Array array(createTestArrayBuffer(bufferSize));
    Uint8ArrayIODevice arrayDevice(array);

    // Read back byte for byte from the device
    QWASMVERIFY(arrayDevice.open(QIODevice::ReadWrite));
    for (int i = 0; i < bufferSize; ++i) {
        char byte;
        arrayDevice.seek(i);
        arrayDevice.read(&byte, 1);
        QWASMCOMPARE(byte, testByteValue(i));
    }

    // Write a different set of bytes
    QWASMCOMPARE(arrayDevice.seek(0), true);
    for (int i = 0; i < bufferSize; ++i) {
        char byte = testByteValue(i + 1);
        arrayDevice.seek(i);
        QWASMCOMPARE(arrayDevice.write(&byte, 1), 1);
    }

    // Verify that the original array was updated
    QByteArray copy = QByteArray::fromEcmaUint8Array(array.val());
    for (int i = 0; i < bufferSize; ++i)
        QWASMCOMPARE(copy.at(i), testByteValue(i + 1));

    arrayDevice.close();
    QWASMSUCCESS();
}

int main(int argc, char **argv)
{
    auto testObject = std::make_shared<WasmIoDevicesTest>();
    QtWasmTest::initTestCase<QCoreApplication>(argc, argv, testObject);
    return 0;
}

#include "iodevices_main.moc"