File: test-serialization.C

package info (click to toggle)
linbox 1.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,940 kB
  • sloc: cpp: 108,392; lisp: 5,469; makefile: 1,345; sh: 1,244; csh: 131; python: 74; perl: 2
file content (225 lines) | stat: -rw-r--r-- 6,101 bytes parent folder | download | duplicates (4)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* Copyright (C) LinBox
*
* ========LICENCE========
* This file is part of the library LinBox.
*
* LinBox is free software: you can redistribute it and/or modify
* it under the terms of the  GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
* ========LICENCE========
*/

/**
 * This is testing the serialization routines,
 * by serializing, then unserializing and checking equality.
 *
 * Basic types (integer and floating points) are checked first.
 * Custom LinBox classes (BlasMatrix, SparseMatrix, ...) are checked too.
 */

#include "linbox/matrix/random-matrix.h"
#include "linbox/util/serialization.h"

using namespace LinBox;

template <class T>
bool test(T& output, const T& input)
{
    // The vector of bytes that will store the serialized input
    // is created with a certain size of uninitialized data.
    // This is used to check that the unserialized does
    // understand the offset start.
    uint64_t randomOffset = rand() % 100;
    std::vector<uint8_t> bytes(randomOffset);

    auto bytesWritten = serialize(bytes, input);
    if (bytesWritten == 0u) {
        return false;
    }

    auto bytesRead = unserialize(output, bytes, randomOffset);
    if (bytesRead != bytesWritten) {
        return false;
    }

    return true;
}

template <class T>
bool check_basic_type(const T& input)
{
    T output;
    if (!test(output, input)) {
        return false;
    }

    if (output != input) {
        return false;
    }

    return true;
}

template <class T>
bool test_basic_type()
{
    // @note This little trick is to ensure that 64 bits type
    // have not just their lower bits written.
    T input = rand();
    input *= rand();
    input += rand();

    return check_basic_type(input);
}

bool test_integer()
{
    // Generating a big integer to ensure it is spreading over multiple limbs.
    Integer input(1);
    for (int i = 0; i < 20; ++i) {
        input *= (1 + rand());
    }
    input += rand();

    return check_basic_type(input);
}

// Check matrix equality after serialize/unserialize.
template <class Field, class Matrix>
bool check_matrix(const Field& F, const Matrix& input)
{
    Matrix output(F);

    if (!test(output, input)) {
        return false;
    }

    if (output.rowdim() != input.rowdim() || output.coldim() != input.coldim()) {
        return false;
    }

    for (auto i = 0u; i < input.rowdim(); i++) {
        for (auto j = 0u; j < input.coldim(); j++) {
            if (!F.areEqual(input.getEntry(i, j), output.getEntry(i, j))) {
                return false;
            }
        }
    }

    return true;
}

// Check vector equality after serialize/unserialize.
template <class Field, class Vector>
bool check_vector(const Field& F, const Vector& input)
{
    Vector output(F);

    if (!test(output, input)) {
        return false;
    }

    if (output.size() != input.size()) {
        return false;
    }

    for (auto i = 0u; i < input.size(); i++) {
        if (!F.areEqual(input[i], output[i])) {
            return false;
        }
    }

    return true;
}

// Tests serialibility of matrices and vectors of specified field elements.
template <class Field>
bool test_field(const Integer& q)
{
    // --- Test dense matrix

    Field F(q);
    BlasMatrix<Field> denseMatrix(F, 10 + rand() % 100, 10 + rand() % 100);

    // Fill denseMatrix with random values!
    typename Field::RandIter R(F);
    RandomDenseMatrix<typename Field::RandIter, Field> RandMat(F, R);
    RandMat.random(denseMatrix);

    check_matrix(F, denseMatrix);

    // --- Test sparse matrix

    SparseMatrix<Field> sparseMatrix(F, denseMatrix.rowdim(), denseMatrix.coldim());
    for (auto i = 0u; i < denseMatrix.rowdim(); i++) {
        for (auto j = 0u; j < denseMatrix.coldim(); j++) {
            if (rand() % 2 == 0) {
                sparseMatrix.setEntry(i, j, denseMatrix.getEntry(i, j));
            }
        }
    }

    check_matrix(F, sparseMatrix);

    // --- Test dense vector

    BlasVector<Field> denseVector(F, denseMatrix.rowdim());
    for (auto i = 0u; i < denseMatrix.rowdim(); i++) {
        denseVector[i] = denseMatrix.getEntry(i, 0);
    }

    check_vector(F, denseVector);

    return true;
}

int main(int argc, char** argv)
{
    Integer q = 101;
    uint64_t seed = time(nullptr);
    bool loop = false;

    Argument as[] = {{'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INTEGER, &q},
                     {'s', "-s seed", "Set seed for the random generator", TYPE_UINT64, &seed},
                     {'l', "-loop Y/N", "run the test in an infinite loop.", TYPE_BOOL, &loop},
                     END_OF_ARGUMENTS};

    FFLAS::parseArguments(argc, argv, as);

    srand(seed);

    bool ok = true;
    do {
        ok = ok && test_basic_type<int8_t>();
        ok = ok && test_basic_type<uint8_t>();
        ok = ok && test_basic_type<int16_t>();
        ok = ok && test_basic_type<uint16_t>();
        ok = ok && test_basic_type<int32_t>();
        ok = ok && test_basic_type<uint32_t>();
        ok = ok && test_basic_type<int64_t>();
        ok = ok && test_basic_type<uint64_t>();
        ok = ok && test_basic_type<float>();
        ok = ok && test_basic_type<double>();

        ok = ok && test_integer();

        ok = ok && test_field<Givaro::ZRing<Integer>>(q);
        ok = ok && test_field<Givaro::Modular<float>>(q);
        ok = ok && test_field<Givaro::Modular<double>>(q);
    } while (loop && ok);

    if (!ok) std::cerr << "Failed with seed: " << seed << std::endl;

    return !ok;
}