summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/tensor/tensor_operations/tensor_operations_test.cpp
blob: b079c4f64c30beee8e1821f86cd03991d1d9cf76 (plain) (blame)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/tensor/simple/simple_tensor.h>
#include <vespa/vespalib/tensor/simple/simple_tensor_builder.h>
#include <vespa/vespalib/tensor/compact/compact_tensor.h>
#include <vespa/vespalib/tensor/compact/compact_tensor_builder.h>
#include <vespa/vespalib/tensor/compact/compact_tensor_v2.h>
#include <vespa/vespalib/tensor/compact/compact_tensor_v2_builder.h>
#include <vespa/vespalib/tensor/types.h>
#include <vespa/vespalib/tensor/tensor_factory.h>
#include <vespa/vespalib/tensor/tensor_function.h>
#include <vespa/vespalib/tensor/tensor_visitor.h>
#include <iostream>

using namespace vespalib::tensor;

namespace vespalib {
namespace tensor {

static bool operator==(const Tensor &lhs, const Tensor &rhs)
{
    return lhs.equals(rhs);
}

}
}

//-----------------------------------------------------------------------------

class MyInput : public TensorFunction::Input
{
private:
    std::vector<Tensor::CREF> tensors;
    std::vector<CellFunction::CREF> cell_functions;
    const Tensor &get_tensor(size_t id) const override {
        ASSERT_GREATER(tensors.size(), id);
        return tensors[id];
    }
    virtual const CellFunction &get_cell_function(size_t id) const override {
        ASSERT_GREATER(cell_functions.size(), id);
        return cell_functions[id];
    }
public:
    size_t add(const Tensor &tensor) {
        size_t id = tensors.size();
        tensors.push_back(tensor);
        return id;
    }
    size_t add(const CellFunction &cell_function) {
        size_t id = cell_functions.size();
        cell_functions.push_back(cell_function);
        return id;
    }
};

const Tensor &eval_tensor(function::Node &function_ir, const TensorFunction::Input &input) {
    ASSERT_TRUE(function_ir.type().is_tensor());
    TensorFunction &function = function_ir; // compile step
    const Tensor &result = function.eval(input).as_tensor;
    EXPECT_EQUAL(result.getType(), function_ir.type());
    return result;
}

const Tensor &eval_tensor_unchecked(function::Node &function_ir, const TensorFunction::Input &input) {
    ASSERT_TRUE(function_ir.type().is_tensor());
    TensorFunction &function = function_ir; // compile step
    return function.eval(input).as_tensor;
}

double eval_number(function::Node &function_ir, const TensorFunction::Input &input) {
    ASSERT_TRUE(function_ir.type().is_number());
    TensorFunction &function = function_ir; // compile step    
    return function.eval(input).as_double;
}

//-----------------------------------------------------------------------------

template <typename BuilderType>
struct Fixture
{
    BuilderType _builder;
    Fixture() : _builder() {}

    Tensor::UP createTensor(const TensorCells &cells) {
        return TensorFactory::create(cells, _builder);
    }
    Tensor::UP createTensor(const TensorCells &cells, const TensorDimensions &dimensions) {
        return TensorFactory::create(cells, dimensions, _builder);
    }
    void assertEquals(const TensorCells &lhs,
                      const TensorDimensions &lhsDimensions,
                      const TensorCells &rhs,
                      const TensorDimensions &rhsDimensions) {
        EXPECT_EQUAL(*createTensor(lhs, lhsDimensions),
                     *createTensor(rhs, rhsDimensions));
    }
    void assertEquals(const TensorCells &lhs, const TensorCells &rhs) {
        EXPECT_EQUAL(*createTensor(lhs), *createTensor(rhs));
    }
    void assertNotEquals(const TensorCells &lhs, const TensorCells &rhs) {
        EXPECT_NOT_EQUAL(*createTensor(lhs), *createTensor(rhs));
    }
    void assertNotEquals(const TensorCells &lhs,
                         const TensorDimensions &lhsDimensions,
                         const TensorCells &rhs,
                         const TensorDimensions &rhsDimensions) {
        EXPECT_NOT_EQUAL(*createTensor(lhs, lhsDimensions),
                         *createTensor(rhs, rhsDimensions));
    }
    void assertAddImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs) {
        MyInput input;
        function::Node_UP ir = function::add(function::input(lhs.getType(), input.add(lhs)),
                                             function::input(rhs.getType(), input.add(rhs)));
        EXPECT_EQUAL(exp, eval_tensor(*ir, input));
    }
    void assertAdd(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs) {
        assertAddImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs));
    }
    void assertSubtractImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs) {
        MyInput input;
        function::Node_UP ir = function::subtract(function::input(lhs.getType(), input.add(lhs)),
                                                  function::input(rhs.getType(), input.add(rhs)));
        EXPECT_EQUAL(exp, eval_tensor(*ir, input));
    }
    void assertSubtract(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs) {
        assertSubtractImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs));
    }
    void assertMinImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs) {
        MyInput input;
        function::Node_UP ir = function::min(function::input(lhs.getType(), input.add(lhs)),
                                             function::input(rhs.getType(), input.add(rhs)));
        EXPECT_EQUAL(exp, eval_tensor(*ir, input));
    }
    void assertMin(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs) {
        assertMinImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs));
    }
    void assertMaxImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs) {
        MyInput input;
        function::Node_UP ir = function::max(function::input(lhs.getType(), input.add(lhs)),
                                             function::input(rhs.getType(), input.add(rhs)));
        EXPECT_EQUAL(exp, eval_tensor(*ir, input));
    }
    void assertMax(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs) {
        assertMaxImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs));
    }
    void assertSumImpl(double exp, const Tensor &tensor) {
        MyInput input;
        function::Node_UP ir = function::sum(function::input(tensor.getType(), input.add(tensor)));
        EXPECT_EQUAL(exp, eval_number(*ir, input));
    }
    void assertSum(double exp, const TensorCells &cells) {
        assertSumImpl(exp, *createTensor(cells));
    }
    void assertMatchImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs) {
        MyInput input;
        function::Node_UP ir = function::match(function::input(lhs.getType(), input.add(lhs)),
                                               function::input(rhs.getType(), input.add(rhs)));
        // The match operation currently ends up the union of input
        // dimensions. It should be the intersection of input
        // dimensions as claimed by the intermediate
        // representation. The tensor result type checking is disabled
        // until the corresponding bug is fixed.
        EXPECT_EQUAL(exp, eval_tensor_unchecked(*ir, input)); // UNCHECKED (ref VESPA-1868)
    }
    void assertMatch(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs) {
        assertMatchImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs));
    }
    void assertMatch(const TensorCells &expTensor, const TensorDimensions &expDimensions,
                     const TensorCells &lhs, const TensorCells &rhs) {
        assertMatchImpl(*createTensor(expTensor, expDimensions), *createTensor(lhs), *createTensor(rhs));
    }
    void assertMultiplyImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs) {
        MyInput input;
        function::Node_UP ir = function::multiply(function::input(lhs.getType(), input.add(lhs)),
                                                  function::input(rhs.getType(), input.add(rhs)));
        EXPECT_EQUAL(exp, eval_tensor(*ir, input));
    }
    void assertMultiply(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs) {
        assertMultiplyImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs));
    }
    void assertMultiply(const TensorCells &expTensor, const TensorDimensions &expDimensions,
                        const TensorCells &lhs, const TensorCells &rhs) {
        assertMultiplyImpl(*createTensor(expTensor, expDimensions), *createTensor(lhs), *createTensor(rhs));
    }
    void assertMultiplyImpl(const Tensor &exp, const Tensor &arg1, const Tensor &arg2, const Tensor &arg3) {
        MyInput input;
        function::Node_UP ir = function::multiply(
                function::multiply(function::input(arg1.getType(), input.add(arg1)),
                                   function::input(arg2.getType(), input.add(arg2))),
                function::input(arg3.getType(), input.add(arg3)));
        EXPECT_EQUAL(exp, eval_tensor(*ir, input));
    }
    void assertMultiply(const TensorCells &expTensor, const TensorDimensions &expDimensions,
                        const TensorCells &arg1, const TensorCells &arg2, const TensorCells &arg3) {
        assertMultiplyImpl(*createTensor(expTensor, expDimensions), *createTensor(arg1), *createTensor(arg2), *createTensor(arg3));
    }
    void assertApplyImpl(const Tensor &exp, const Tensor &tensor, const CellFunction &func) {
        MyInput input;
        function::Node_UP ir = function::apply(function::input(tensor.getType(), input.add(tensor)), input.add(func));
        EXPECT_EQUAL(exp, eval_tensor(*ir, input));
    }
    void assertApply(const TensorCells &exp, const TensorCells &arg, const CellFunction &func) {
        assertApplyImpl(*createTensor(exp), *createTensor(arg), func);
    }
    void assertDimensionSumImpl(const Tensor &exp, const Tensor &tensor, const vespalib::string &dimension) {
        MyInput input;
        function::Node_UP ir = function::dimension_sum(function::input(tensor.getType(), input.add(tensor)), dimension);
        EXPECT_EQUAL(exp, eval_tensor(*ir, input));
    }
    void assertDimensionSum(const TensorCells &exp, const TensorCells &arg,
                            const vespalib::string &dimension) {
        assertDimensionSumImpl(*createTensor(exp), *createTensor(arg), dimension);
    }
};

using SimpleFixture = Fixture<SimpleTensorBuilder>;
using CompactFixture = Fixture<CompactTensorBuilder>;
using CompactV2Fixture = Fixture<CompactTensorV2Builder>;


template <typename FixtureType>
void
testTensorEquals(FixtureType &f)
{
    TEST_DO(f.assertEquals({}, {}));
    TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {}));
    TEST_DO(f.assertNotEquals({}, { {{{"x","1"}}, 3} }));
    TEST_DO(f.assertEquals({ {{{"x","1"}}, 3} }, { {{{"x","1"}}, 3} }));
    TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, { {{{"x","1"}}, 4} }));
    TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, { {{{"x","2"}}, 3} }));
    TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, { {{{"y","1"}}, 3} }));
    TEST_DO(f.assertEquals({ {{{"x","1"}}, 3} }, {"x"},
                           { {{{"x","1"}}, 3} }, {"x"}));
    TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {"x"},
                              { {{{"x","1"}}, 4} }, {"x"}));
    TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {"x"},
                              { {{{"x","2"}}, 3} }, {"x"}));
    TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {"x"},
                              { {{{"x","2"}}, 3} }, {"x"}));
    TEST_DO(f.assertEquals({ {{{"x","1"}}, 3} }, {"x", "y"},
                           { {{{"x","1"}}, 3} }, {"x", "y"}));
    TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {"x", "y"},
                              { {{{"x","1"}}, 3} }, {"x", "z"}));
    TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {"x", "y"},
                              { {{{"y","1"}}, 3} }, {"y", "z"}));
}

template <typename FixtureType>
void
testTensorAdd(FixtureType &f)
{
    f.assertAdd({},{},{});
    f.assertAdd({ {{{"x","1"}}, 3}, {{{"x","2"}}, 5} },
                { {{{"x","1"}}, 3} },
                { {{{"x","2"}}, 5} });
    f.assertAdd({ {{{"x","1"}}, 8} },
                { {{{"x","1"}}, 3} },
                { {{{"x","1"}}, 5} });
    f.assertAdd({ {{{"x","1"}}, -2} },
                { {{{"x","1"}}, 3} },
                { {{{"x","1"}}, -5} });
    f.assertAdd({ {{{"x","1"}}, 0} },
                { {{{"x","1"}}, 3} },
                { {{{"x","1"}}, -3} });
    f.assertAdd({ {{{"x","1"}}, 3}, {{{"y","2"}}, 12}, {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} });
    f.assertAdd({ {{{"x","1"}}, 3}, {{{"y","2"}}, 12}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} });
    f.assertAdd({ {{{"y","2"}}, 12}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 5} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} });
    f.assertAdd({ {{{"y","2"}}, 12}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 5} });
    f.assertAdd({ {{{"x","1"}}, 3}, {{{"y","2"}}, 12} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                { {{{"y","2"}}, 7} });
    f.assertAdd({ {{{"x","1"}}, 3}, {{{"y","2"}}, 12} },
                { {{{"y","2"}}, 7} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} });
    f.assertAdd({ {{{"x","1"}}, 3}, {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3} },
                { {{{"z","3"}}, 11} });
    f.assertAdd({ {{{"x","1"}}, 3}, {{{"z","3"}}, 11} },
                { {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3} });
}

template <typename FixtureType>
void
testTensorSubtract(FixtureType &f)
{
    f.assertSubtract({},{},{});
    f.assertSubtract({ {{{"x","1"}}, 3}, {{{"x","2"}}, -5} },
                     { {{{"x","1"}}, 3} },
                     { {{{"x","2"}}, 5} });
    f.assertSubtract({ {{{"x","1"}}, -2} },
                     { {{{"x","1"}}, 3} },
                     { {{{"x","1"}}, 5} });
    f.assertSubtract({ {{{"x","1"}}, 8} },
                     { {{{"x","1"}}, 3} },
                     { {{{"x","1"}}, -5} });
    f.assertSubtract({ {{{"x","1"}}, 0} },
                     { {{{"x","1"}}, 3} },
                     { {{{"x","1"}}, 3} });
    f.assertSubtract({ {{{"x","1"}}, 3}, {{{"y","2"}},-2}, {{{"z","3"}},-11} },
                     { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                     { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} });
    f.assertSubtract({ {{{"x","1"}},-3}, {{{"y","2"}}, 2}, {{{"z","3"}}, 11} },
                     { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                     { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} });
    f.assertSubtract({ {{{"y","2"}},-2}, {{{"z","3"}},-11} },
                     { {{{"y","2"}}, 5} },
                     { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} });
    f.assertSubtract({ {{{"y","2"}}, 2}, {{{"z","3"}}, 11} },
                     { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                     { {{{"y","2"}}, 5} });
    f.assertSubtract({ {{{"x","1"}}, 3}, {{{"y","2"}},-2} },
                     { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                     { {{{"y","2"}}, 7} });
    f.assertSubtract({ {{{"x","1"}},-3}, {{{"y","2"}}, 2} },
                     { {{{"y","2"}}, 7} },
                     { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} });
    f.assertSubtract({ {{{"x","1"}}, 3}, {{{"z","3"}},-11} },
                     { {{{"x","1"}}, 3} },
                     { {{{"z","3"}}, 11} });
    f.assertSubtract({ {{{"x","1"}},-3}, {{{"z","3"}}, 11} },
                     { {{{"z","3"}}, 11} },
                     { {{{"x","1"}}, 3} });
}

template <typename FixtureType>
void
testTensorMin(FixtureType &f)
{
    f.assertMin({},{},{});
    f.assertMin({ {{{"x","1"}}, 3}, {{{"x","2"}}, 5} },
                { {{{"x","1"}}, 3} },
                { {{{"x","2"}}, 5} });
    f.assertMin({ {{{"x","1"}}, 3} },
                { {{{"x","1"}}, 3} },
                { {{{"x","1"}}, 5} });
    f.assertMin({ {{{"x","1"}}, -5} },
                { {{{"x","1"}}, 3} },
                { {{{"x","1"}}, -5} });
    f.assertMin({ {{{"x","1"}}, 3}, {{{"x","2"}}, 0} },
                { {{{"x","1"}}, 3} },
                { {{{"x","2"}}, 0} });
    f.assertMin({ {{{"x","1"}}, 3}, {{{"y","2"}}, 5}, {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} });
    f.assertMin({ {{{"x","1"}}, 3}, {{{"y","2"}}, 5}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} });
    f.assertMin({ {{{"y","2"}}, 5}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 5} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} });
    f.assertMin({ {{{"y","2"}}, 5}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 5} });
    f.assertMin({ {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                { {{{"y","2"}}, 7} });
    f.assertMin({ {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                { {{{"y","2"}}, 7} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} });
    f.assertMin({ {{{"x","1"}}, 3}, {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3} },
                { {{{"z","3"}}, 11} });
    f.assertMin({ {{{"x","1"}}, 3}, {{{"z","3"}}, 11} },
                { {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3} });
}

template <typename FixtureType>
void
testTensorMax(FixtureType &f)
{
    f.assertMax({},{},{});
    f.assertMax({ {{{"x","1"}}, 3}, {{{"x","2"}}, 5} },
                { {{{"x","1"}}, 3} },
                { {{{"x","2"}}, 5} });
    f.assertMax({ {{{"x","1"}}, 5} },
                { {{{"x","1"}}, 3} },
                { {{{"x","1"}}, 5} });
    f.assertMax({ {{{"x","1"}}, 3} },
                { {{{"x","1"}}, 3} },
                { {{{"x","1"}}, -5} });
    f.assertMax({ {{{"x","1"}}, 3}, {{{"x","2"}}, 0} },
                { {{{"x","1"}}, 3} },
                { {{{"x","2"}}, 0} });
    f.assertMax({ {{{"x","1"}}, 3}, {{{"x","2"}}, -5} },
                { {{{"x","1"}}, 3} },
                { {{{"x","2"}}, -5} });
    f.assertMax({ {{{"x","1"}}, 3}, {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} });
    f.assertMax({ {{{"x","1"}}, 3}, {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} });
    f.assertMax({ {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 5} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} });
    f.assertMax({ {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                { {{{"y","2"}}, 5} });
    f.assertMax({ {{{"x","1"}}, 3}, {{{"y","2"}}, 7} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                { {{{"y","2"}}, 7} });
    f.assertMax({ {{{"x","1"}}, 3}, {{{"y","2"}}, 7} },
                { {{{"y","2"}}, 7} },
                { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} });
    f.assertMax({ {{{"x","1"}}, 3}, {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3} },
                { {{{"z","3"}}, 11} });
    f.assertMax({ {{{"x","1"}}, 3}, {{{"z","3"}}, 11} },
                { {{{"z","3"}}, 11} },
                { {{{"x","1"}}, 3} });
}

template <typename FixtureType>
void
testTensorSum(FixtureType &f)
{
    f.assertSum(0.0, {});
    f.assertSum(0.0, { {{{"x","1"}}, 0} });
    f.assertSum(3.0, { {{{"x","1"}}, 3} });
    f.assertSum(8.0, { {{{"x","1"}}, 3}, {{{"x","2"}}, 5} });
    f.assertSum(-2.0, { {{{"x","1"}}, 3}, {{{"x","2"}}, -5} });
}

template <typename FixtureType>
void
testTensorMatch(FixtureType &f)
{
    TEST_DO(f.assertMatch({}, {}, {}));
    TEST_DO(f.assertMatch({}, {"x"},
                          { {{{"x","1"}}, 3} },
                          { {{{"x","2"}}, 5} }));
    TEST_DO(f.assertMatch({ {{{"x","1"}}, 15} },
                          { {{{"x","1"}}, 3} },
                          { {{{"x","1"}}, 5} }));
    TEST_DO(f.assertMatch({ {{{"x","1"}}, 0} },
                          { {{{"x","1"}}, 3} },
                          { {{{"x","1"}}, 0} }));
    TEST_DO(f.assertMatch({ {{{"x","1"}}, -15} },
                          { {{{"x","1"}}, 3} },
                          { {{{"x","1"}}, -5} }));
    TEST_DO(f.assertMatch({ {{{"x","1"}}, 15},
                            {{{"x","1"}, {"y","1"}}, 7} }, {"x","y","z"},
                          { {{{"x","1"}}, 3},
                            {{{"x","2"}}, 3},
                            {{{"x","1"},{"y","1"}}, 1},
                            {{{"x","1"},{"y","2"}}, 6} },
                          { {{{"x","1"}}, 5},
                            {{{"x","1"},{"y","1"}}, 7},
                            {{{"x","1"},{"y","1"},{"z","1"}}, 6} }));
    TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"x", "y", "z"},
                          { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                          { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }));
    TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"x", "y", "z"},
                          { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                          { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }));
    TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"y", "z"},
                          { {{{"y","2"}}, 5} },
                          { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }));
    TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"y", "z"},
                          { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} },
                          { {{{"y","2"}}, 5} }));
    TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"x", "y"},
                          { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} },
                          { {{{"y","2"}}, 7} }));
    TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"x", "y"},
                          { {{{"y","2"}}, 7} },
                          { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }));
    TEST_DO(f.assertMatch({ }, {"x", "z"},
                          { {{{"x","1"}}, 3} },
                          { {{{"z","3"}}, 11} }));
    TEST_DO(f.assertMatch({ }, {"x", "z"},
                          { {{{"z","3"}}, 11} },
                          { {{{"x","1"}}, 3} }));
}

template <typename FixtureType>
void
testTensorMultiply(FixtureType &f)
{
    f.assertMultiply({}, {}, {});
    f.assertMultiply({}, {"x"},
                     { {{{"x","1"}}, 3} },
                     { {{{"x","2"}}, 5} });
    f.assertMultiply({ {{{"x","1"}}, 15} },
                     { {{{"x","1"}}, 3} },
                     { {{{"x","1"}}, 5} });
    f.assertMultiply({ {{{"x","1"},{"y","1"}}, 15} },
                     { {{{"x","1"}}, 3} },
                     { {{{"y","1"}}, 5} });
    f.assertMultiply({ {{{"x","1"},{"y","1"}}, 15}, {{{"x","2"},{"y","1"}}, 35} },
                     { {{{"x","1"}}, 3}, {{{"x","2"}}, 7} },
                     { {{{"y","1"}}, 5} });
    f.assertMultiply({ {{{"x","1"},{"y","1"},{"z","1"}}, 7},
                       {{{"x","1"},{"y","1"},{"z","2"}}, 13},
                       {{{"x","2"},{"y","1"},{"z","1"}}, 21},
                       {{{"x","2"},{"y","1"},{"z","2"}}, 39},
                       {{{"x","1"},{"y","2"},{"z","1"}}, 55} },
                     { {{{"x","1"},{"y","1"}}, 1},
                       {{{"x","2"},{"y","1"}}, 3},
                       {{{"x","1"},{"y","2"}}, 5} },
                     { {{{"y","1"},{"z","1"}}, 7},
                       {{{"y","2"},{"z","1"}}, 11},
                       {{{"y","1"},{"z","2"}}, 13} });
    f.assertMultiply({ {{{"x","1"},{"y","1"},{"z","1"}}, 7} },
                     { {{{"x","1"}}, 5}, {{{"x","1"},{"y","1"}}, 1} },
                     { {{{"y","1"},{"z","1"}}, 7} });
    f.assertMultiply({ {{{"x","1"},{"y","1"},{"z","1"}}, 7}, {{{"x","1"},{"z","1"}}, 55} },
                     { {{{"x","1"}}, 5}, {{{"x","1"},{"y","1"}}, 1} },
                     { {{{"z","1"}}, 11}, {{{"y","1"},{"z","1"}}, 7} });
    f.assertMultiply({ {{{"x","1"},{"y","1"},{"z","1"}}, 7} },
                     { {{}, 5}, {{{"x","1"},{"y","1"}}, 1} },
                     { {{{"y","1"},{"z","1"}}, 7} });
    f.assertMultiply({ {{{"x","1"},{"y","1"},{"z","1"}}, 7}, {{}, 55} },
                     { {{}, 5}, {{{"x","1"},{"y","1"}}, 1} },
                     { {{}, 11}, {{{"y","1"},{"z","1"}}, 7} });
}

template <typename FixtureType>
void
testTensorMultiplePreservationOfDimensions(FixtureType &f)
{
    f.assertMultiply({}, {"x"},
                     { {{{"x","1"}}, 1} },
                     { {{{"x","2"}}, 1} });
    f.assertMultiply({ {{{"x","1"}}, 1} }, {"x","y"},
                     { {{{"x","1"}}, 1} },
                     { {{{"x","2"},{"y","1"}}, 1}, {{{"x","1"}}, 1} });
    f.assertMultiply({}, {"x","y"},
                     { {{{"x","1"}}, 1} },
                     { {{{"x","2"},{"y","1"}}, 1}, {{{"x","1"}}, 1} },
                     { {{{"x","1"},{"y","1"}}, 1} });
    f.assertMultiply({ {{{"x","1"},{"y","1"}}, 1} }, {"x","y"},
                     { {{{"x","1"}}, 1} },
                     { {{{"x","1"},{"y","1"}}, 1} });
}

struct MyFunction : public CellFunction
{
    virtual double apply(double value) const override {
        return value + 5;
    }
};

template <typename FixtureType>
void
testTensorApply(FixtureType &f)
{
    f.assertApply({ {{{"x","1"}}, 6}, {{{"y","1"}}, 2} },
                  { {{{"x","1"}}, 1}, {{{"y","1"}}, -3} },
                  MyFunction());
}

template <typename FixtureType>
void
testTensorSumDimension(FixtureType &f)
{
    f.assertDimensionSum({ {{{"y","1"}}, 4}, {{{"y","2"}}, 12} },
                         { {{{"x","1"},{"y","1"}}, 1},
                           {{{"x","2"},{"y","1"}}, 3},
                           {{{"x","1"},{"y","2"}}, 5},
                           {{{"x","2"},{"y","2"}}, 7} }, "x");
    f.assertDimensionSum({ {{{"x","1"}}, 6}, {{{"x","2"}}, 10} },
                         { {{{"x","1"},{"y","1"}}, 1},
                           {{{"x","2"},{"y","1"}}, 3},
                           {{{"x","1"},{"y","2"}}, 5},
                           {{{"x","2"},{"y","2"}}, 7} }, "y");
    f.assertDimensionSum({ {{}, 13}, {{{"x","1"}}, 17}, {{{"x","2"}}, 10} },
                         { {{{"x","1"},{"y","1"}}, 1},
                           {{{"x","2"},{"y","1"}}, 3},
                           {{{"x","1"},{"y","2"}}, 5},
                           {{{"x","2"},{"y","2"}}, 7},
                           {{{"x","1"}}, 11},
                           {{{"y","2"}}, 13} }, "y");
    f.assertDimensionSum({ {{}, 11}, {{{"y","1"}}, 4}, {{{"y","2"}}, 25}, {{{"z","1"}}, 19} },
                         { {{{"x","1"},{"y","1"}}, 1},
                           {{{"x","2"},{"y","1"}}, 3},
                           {{{"x","1"},{"y","2"}}, 5},
                           {{{"x","2"},{"y","2"}}, 7},
                           {{{"x","1"}}, 11},
                           {{{"y","2"}}, 13},
                           {{{"z","1"}}, 19}, }, "x");
}

template <typename FixtureType>
void
testAllTensorOperations(FixtureType &f)
{
    TEST_DO(testTensorEquals(f));
    TEST_DO(testTensorAdd(f));
    TEST_DO(testTensorSubtract(f));
    TEST_DO(testTensorMin(f));
    TEST_DO(testTensorMax(f));
    TEST_DO(testTensorSum(f));
    TEST_DO(testTensorMatch(f));
    TEST_DO(testTensorMultiply(f));
    TEST_DO(testTensorMultiplePreservationOfDimensions(f));
    TEST_DO(testTensorApply(f));
    TEST_DO(testTensorSumDimension(f));
}

TEST_F("test tensor operations for SimpleTensor", SimpleFixture)
{
    testAllTensorOperations(f);
}

TEST_F("test tensor operations for CompactTensor", CompactFixture)
{
    testAllTensorOperations(f);
}

TEST_F("test tensor operations for CompactTensorV2", CompactV2Fixture)
{
    testAllTensorOperations(f);
}

TEST_MAIN() { TEST_RUN_ALL(); }