aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/tensor/TypeResolverTestCase.java
blob: 82b7e73fb20a61441c12644d76064e93f3c5d120 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.tensor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * @author arnej
 */
public class TypeResolverTestCase {

    private static List<String> mkl(String ...values) {
        return Arrays.asList(values);
    }

    @Test
    public void verifyMap() {
        checkMap("tensor()", "tensor()");
        checkMap("tensor(x[10])", "tensor(x[10])");
        checkMap("tensor(a[10],b[20],c[30])", "tensor(a[10],b[20],c[30])");
        checkMap("tensor(y{})", "tensor(y{})");
        checkMap("tensor(x[10],y{})", "tensor(x[10],y{})");
        checkMap("tensor<float>(x[10])", "tensor<float>(x[10])");
        checkMap("tensor<float>(y{})", "tensor<float>(y{})");
        checkMap("tensor<bfloat16>(x[10])", "tensor<float>(x[10])");
        checkMap("tensor<bfloat16>(y{})", "tensor<float>(y{})");
        checkMap("tensor<int8>(x[10])", "tensor<float>(x[10])");
        checkMap("tensor<int8>(y{})", "tensor<float>(y{})");
    }

    @Test
    public void verifyJoin() {
        checkJoin("tensor()",                    "tensor()",                                       "tensor()");
        checkJoin("tensor()",                    "tensor(x{})",                                    "tensor(x{})");
        checkJoin("tensor(x{})",                 "tensor()",                                       "tensor(x{})");
        checkJoin("tensor(x{})",                 "tensor(x{})",                                    "tensor(x{})");
        checkJoin("tensor(x{})",                 "tensor(y{})",                                    "tensor(x{},y{})");
        checkJoin("tensor(x{},y{})",             "tensor(y{},z{})",                                "tensor(x{},y{},z{})");
        checkJoin("tensor(y{})",                 "tensor()",                                       "tensor(y{})");
        checkJoin("tensor(y{})",                 "tensor(y{})",                                    "tensor(y{})");
        checkJoin("tensor(a[10])",               "tensor(a[10])",                                  "tensor(a[10])");
        checkJoin("tensor(a[10])",               "tensor()",                                       "tensor(a[10])");
        checkJoin("tensor(a[10])",               "tensor(x{},y{},z{})",                            "tensor(a[10],x{},y{},z{})");
        // with cell types
        checkJoin("tensor<bfloat16>(x[5])",      "tensor<bfloat16>(x[5])",                         "tensor<float>(x[5])");
        checkJoin("tensor<bfloat16>(x[5])",      "tensor<float>(x[5])",                            "tensor<float>(x[5])");
        checkJoin("tensor<bfloat16>(x[5])",      "tensor<int8>(x[5])",                             "tensor<float>(x[5])");
        checkJoin("tensor<bfloat16>(x[5])",      "tensor()",                                       "tensor<float>(x[5])");
        checkJoin("tensor<bfloat16>(x[5])",      "tensor(x[5])",                                   "tensor(x[5])");
        checkJoin("tensor<bfloat16>(x{})",       "tensor<bfloat16>(y{})",                          "tensor<float>(x{},y{})");
        checkJoin("tensor<bfloat16>(x{})",       "tensor<int8>(y{})",                              "tensor<float>(x{},y{})");
        checkJoin("tensor<bfloat16>(x{})",       "tensor()",                                       "tensor<float>(x{})");
        checkJoin("tensor<float>(x[5])",         "tensor<float>(x[5])",                            "tensor<float>(x[5])");
        checkJoin("tensor<float>(x[5])",         "tensor<int8>(x[5])",                             "tensor<float>(x[5])");
        checkJoin("tensor<float>(x[5])",         "tensor()",                                       "tensor<float>(x[5])");
        checkJoin("tensor<float>(x[5])",         "tensor(x[5])",                                   "tensor(x[5])");
        checkJoin("tensor<float>(x{})",          "tensor<bfloat16>(y{})",                          "tensor<float>(x{},y{})");
        checkJoin("tensor<float>(x{})",          "tensor<float>(y{})",                             "tensor<float>(x{},y{})");
        checkJoin("tensor<float>(x{})",          "tensor<int8>(y{})",                              "tensor<float>(x{},y{})");
        checkJoin("tensor<float>(x{})",          "tensor()",                                       "tensor<float>(x{})");
        checkJoin("tensor<int8>(x[5])",          "tensor<int8>(x[5])",                             "tensor<float>(x[5])");
        checkJoin("tensor<int8>(x{})",           "tensor<int8>(y{})",                              "tensor<float>(x{},y{})");
        checkJoin("tensor<int8>(x{})",           "tensor()",                                       "tensor<float>(x{})");
        checkJoin("tensor()",                    "tensor<int8>(x[5])",                             "tensor<float>(x[5])");
        checkJoin("tensor(x[5])",                "tensor<int8>(x[5])",                             "tensor(x[5])");
        checkJoin("tensor(x[5])",                "tensor(x[5])",                                   "tensor(x[5])");
        checkJoin("tensor(x{})",                 "tensor<bfloat16>(y{})",                          "tensor(x{},y{})");
        checkJoin("tensor(x{})",                 "tensor<float>(y{})",                             "tensor(x{},y{})");
        checkJoin("tensor(x{})",                 "tensor<int8>(y{})",                              "tensor(x{},y{})");
        // specific for Java
        checkJoin("tensor(x[])",                 "tensor(x{})",                                    "tensor(x{})");
        checkJoin("tensor(x[3])",                "tensor(x{})",                                    "tensor(x{})");
        checkJoin("tensor(x{})",                 "tensor(x[])",                                    "tensor(x{})");
        checkJoin("tensor(x{})",                 "tensor(x[3])",                                   "tensor(x{})");
        // dimension mismatch should fail:
        checkJoinFails("tensor(x[3])",           "tensor(x[5])");
        checkJoinFails("tensor(x[5])",           "tensor(x[3])");
    }

    @Test
    public void verifyReduce() {
        checkFullReduce("tensor()");
        checkReduce("tensor(x[10],y[20],z[30])", mkl("x"), "tensor(y[20],z[30])");
        checkReduce("tensor(x[10],y[20],z[30])", mkl("y"), "tensor(x[10],z[30])");
        checkReduce("tensor<float>(x[10],y[20],z[30])", mkl("z"), "tensor<float>(x[10],y[20])");
        checkReduce("tensor<bfloat16>(x[10],y[20],z[30])", mkl("z"), "tensor<float>(x[10],y[20])");
        checkReduce("tensor<int8>(x[10],y[20],z[30])", mkl("z"), "tensor<float>(x[10],y[20])");
        checkReduce("tensor(x[10],y[20],z[30])", mkl("x", "z"), "tensor(y[20])");
        checkReduce("tensor<float>(x[10],y[20],z[30])", mkl("z", "x"), "tensor<float>(y[20])");
        checkReduce("tensor<bfloat16>(x[10],y[20],z[30])", mkl("z", "x"), "tensor<float>(y[20])");
        checkReduce("tensor<int8>(x[10],y[20],z[30])", mkl("z", "x"), "tensor<float>(y[20])");
        checkFullReduce("tensor(x[10],y[20],z[30])");
        checkFullReduce("tensor<float>(x[10],y[20],z[30])");
        checkFullReduce("tensor<bfloat16>(x[10],y[20],z[30])");
        checkFullReduce("tensor<int8>(x[10],y[20],z[30])");
        checkReduce("tensor(x[10],y[20],z[30])", mkl("x", "y", "z"), "tensor()");
        checkReduce("tensor<float>(x[10],y[20],z[30])", mkl("x", "y", "z"), "tensor()");
        checkReduce("tensor<bfloat16>(x[10],y[20],z[30])", mkl("x", "y", "z"), "tensor()");
        checkReduce("tensor<int8>(x[10],y[20],z[30])", mkl("x", "y", "z"), "tensor()");
        checkReduce("tensor(x[10],y{},z[30])", mkl("x"), "tensor(y{},z[30])");
        checkReduce("tensor(x[10],y{},z[30])", mkl("y"), "tensor(x[10],z[30])");
        checkReduce("tensor<float>(x[10],y{},z[30])", mkl("z"), "tensor<float>(x[10],y{})");
        checkReduce("tensor<bfloat16>(x[10],y{},z[30])", mkl("z"), "tensor<float>(x[10],y{})");
        checkReduce("tensor<int8>(x[10],y{},z[30])", mkl("z"), "tensor<float>(x[10],y{})");
        checkReduce("tensor(x[10],y{},z[30])", mkl("x", "z"), "tensor(y{})");
        checkReduce("tensor<float>(x[10],y{},z[30])", mkl("z", "x"), "tensor<float>(y{})");
        checkReduce("tensor<bfloat16>(x[10],y{},z[30])", mkl("z", "x"), "tensor<float>(y{})");
        checkReduce("tensor<int8>(x[10],y{},z[30])", mkl("z", "x"), "tensor<float>(y{})");
        checkFullReduce("tensor(x[10],y{},z[30])");
        checkFullReduce("tensor<float>(x[10],y{},z[30])");
        checkFullReduce("tensor<bfloat16>(x[10],y{},z[30])");
        checkFullReduce("tensor<int8>(x[10],y{},z[30])");
        checkReduce("tensor(x[10],y{},z[30])", mkl("x", "y", "z"), "tensor()");
        checkReduce("tensor<float>(x[10],y{},z[30])", mkl("x", "y", "z"), "tensor()");
        checkReduce("tensor<bfloat16>(x[10],y{},z[30])", mkl("x", "y", "z"), "tensor()");
        checkReduce("tensor<int8>(x[10],y{},z[30])", mkl("x", "y", "z"), "tensor()");
        // for now, these will just log a warning
        //checkReduceFails("tensor()", "x");
        //checkReduceFails("tensor(y{})", "x");
        //checkReduceFails("tensor<float>(y[10])", "x");
        //checkReduceFails("tensor<int8>(y[10])", "x");
        checkReduce("tensor()", mkl("x"), "tensor()");
        checkReduce("tensor(y{})", mkl("x"), "tensor(y{})");
        checkReduce("tensor<float>(y[10])", mkl("x"), "tensor<float>(y[10])");
        checkReduce("tensor<int8>(y[10])", mkl("x"), "tensor<float>(y[10])");
    }

    @Test
    public void verifyMerge() {
        checkMerge("tensor(a[10])",               "tensor(a[10])",                                  "tensor(a[10])");
        checkMerge("tensor<bfloat16>(x[5])",      "tensor<bfloat16>(x[5])",                         "tensor<float>(x[5])");
        checkMerge("tensor<bfloat16>(x[5])",      "tensor<float>(x[5])",                            "tensor<float>(x[5])");
        checkMerge("tensor<bfloat16>(x[5])",      "tensor<int8>(x[5])",                             "tensor<float>(x[5])");
        checkMerge("tensor<bfloat16>(x[5])",      "tensor(x[5])",                                   "tensor(x[5])");
        checkMerge("tensor<bfloat16>(y{})",       "tensor<bfloat16>(y{})",                          "tensor<float>(y{})");
        checkMerge("tensor<bfloat16>(y{})",       "tensor<int8>(y{})",                              "tensor<float>(y{})");
        checkMerge("tensor<float>(x[5])",         "tensor<float>(x[5])",                            "tensor<float>(x[5])");
        checkMerge("tensor<float>(x[5])",         "tensor<int8>(x[5])",                             "tensor<float>(x[5])");
        checkMerge("tensor<float>(x[5])",         "tensor(x[5])",                                   "tensor(x[5])");
        checkMerge("tensor<float>(y{})",          "tensor<bfloat16>(y{})",                          "tensor<float>(y{})");
        checkMerge("tensor<float>(y{})",          "tensor<float>(y{})",                             "tensor<float>(y{})");
        checkMerge("tensor<float>(y{})",          "tensor<int8>(y{})",                              "tensor<float>(y{})");
        checkMerge("tensor<int8>(x[5])",          "tensor<int8>(x[5])",                             "tensor<float>(x[5])");
        checkMerge("tensor<int8>(y{})",           "tensor<int8>(y{})",                              "tensor<float>(y{})");
        checkMerge("tensor()",                    "tensor()",                                       "tensor()");
        checkMerge("tensor(x[5])",                "tensor<int8>(x[5])",                             "tensor(x[5])");
        checkMerge("tensor(x[5])",                "tensor(x[5])",                                   "tensor(x[5])");
        checkMerge("tensor(x{})",                 "tensor(x{})",                                    "tensor(x{})");
        checkMerge("tensor(x{},y{})",             "tensor<bfloat16>(x{},y{})",                      "tensor(x{},y{})");
        checkMerge("tensor(x{},y{})",             "tensor<float>(x{},y{})",                         "tensor(x{},y{})");
        checkMerge("tensor(x{},y{})",             "tensor<int8>(x{},y{})",                          "tensor(x{},y{})");
        checkMerge("tensor(y{})",                 "tensor(y{})",                                    "tensor(y{})");
        checkMerge("tensor(x{})",                 "tensor(x[5])",                                   "tensor(x{})");
        checkMergeFails("tensor(a[10])",          "tensor()");
        checkMergeFails("tensor(a[10])",          "tensor(x{},y{},z{})");
        checkMergeFails("tensor<bfloat16>(x[5])", "tensor()");
        checkMergeFails("tensor<bfloat16>(x{})",  "tensor()");
        checkMergeFails("tensor<float>(x[5])",    "tensor()");
        checkMergeFails("tensor<float>(x{})",     "tensor()");
        checkMergeFails("tensor<int8>(x{})",      "tensor()");
        checkMergeFails("tensor()",               "tensor<int8>(x[5])");
        checkMergeFails("tensor()",               "tensor(x{})");
        checkMergeFails("tensor(x[3])",           "tensor(x[5])");
        checkMergeFails("tensor(x[5])",           "tensor(x[3])");
        checkMergeFails("tensor(x{})",            "tensor()");
        checkMergeFails("tensor(x{},y{})",        "tensor(x{},z{})");
        checkMergeFails("tensor(y{})",            "tensor()");
    }

    @Test
    public void verifyRename() {
        checkRename("tensor(x[10],y[20],z[30])", mkl("y"), mkl("a"), "tensor(a[20],x[10],z[30])");
        checkRename("tensor(x{})", mkl("x"), mkl("y"), "tensor(y{})");
        checkRename("tensor(x{},y[5])", mkl("x","y"), mkl("y","x"), "tensor(x[5],y{})");
        checkRename("tensor(x[10],y[20],z[30])", mkl("x", "y", "z"), mkl("c", "a", "b"), "tensor(a[20],b[30],c[10])");
        checkRename("tensor(x{})", mkl("x"), mkl("x"), "tensor(x{})");
        checkRename("tensor(x{})", mkl("x"), mkl("y"), "tensor(y{})");
        checkRename("tensor<float>(x{})", mkl("x"), mkl("y"), "tensor<float>(y{})");
        checkRename("tensor<bfloat16>(x{})", mkl("x"), mkl("y"), "tensor<bfloat16>(y{})");
        checkRename("tensor<int8>(x{})", mkl("x"), mkl("y"), "tensor<int8>(y{})");

        checkRenameFails("tensor(x{})", mkl(), mkl());
        checkRenameFails("tensor()", mkl(), mkl());
        checkRenameFails("tensor(x{},y{})", mkl("x"), mkl("y","z"));
        checkRenameFails("tensor(x{},y{})", mkl("x","y"), mkl("z"));
        checkRenameFails("tensor(x[10],y[20],z[30])", mkl("y","z"), mkl("a", "x"));

        // allowed (with warning) for now:
        checkRename("tensor()", mkl("a"), mkl("b"), "tensor()");
        checkRename("tensor(x{},y[10])", mkl("a"), mkl("b"), "tensor(x{},y[10])");
        //checkRenameFails("tensor()", mkl("a"), mkl("b"));

    }

    @Test
    public void verifyConcat() {
        // types can be concatenated
        checkConcat("tensor(y[7])",      "tensor(x{})",  "z", "tensor(x{},y[7],z[2])");
        checkConcat("tensor()",          "tensor()",     "x", "tensor(x[2])");
        checkConcat("tensor(x[2])",      "tensor()",     "x", "tensor(x[3])");
        checkConcat("tensor(x[3])",      "tensor(x[2])", "x", "tensor(x[5])");
        checkConcat("tensor(x[2])",      "tensor()",     "y", "tensor(x[2],y[2])");
        checkConcat("tensor(x[2])",      "tensor(x[2])", "y", "tensor(x[2],y[2])");
        checkConcat("tensor(x[2],y[2])", "tensor(x[3])", "x", "tensor(x[5],y[2])");
        checkConcat("tensor(x[2],y[2])", "tensor(y[7])", "y", "tensor(x[2],y[9])");
        checkConcat("tensor(x[5])",      "tensor(y[7])", "z", "tensor(x[5],y[7],z[2])");
        // cell type is handled correctly for concat
        checkConcat("tensor(x[3])", "tensor(x[2])",                     "x", "tensor(x[5])");
        checkConcat("tensor(x[3])", "tensor<float>(x[2])",              "x", "tensor(x[5])");
        checkConcat("tensor(x[3])", "tensor<bfloat16>(x[2])",           "x", "tensor(x[5])");
        checkConcat("tensor(x[3])", "tensor<int8>(x[2])",               "x", "tensor(x[5])");
        checkConcat("tensor<float>(x[3])", "tensor<float>(x[2])",       "x", "tensor<float>(x[5])");
        checkConcat("tensor<float>(x[3])", "tensor<bfloat16>(x[2])",    "x", "tensor<float>(x[5])");
        checkConcat("tensor<float>(x[3])", "tensor<int8>(x[2])",        "x", "tensor<float>(x[5])");
        checkConcat("tensor<bfloat16>(x[3])", "tensor<bfloat16>(x[2])", "x", "tensor<bfloat16>(x[5])");
        checkConcat("tensor<bfloat16>(x[3])", "tensor<int8>(x[2])",     "x", "tensor<float>(x[5])");
        checkConcat("tensor<int8>(x[3])", "tensor<int8>(x[2])",         "x", "tensor<int8>(x[5])");
        // concat with number preserves cell type
        checkConcat("tensor(x[3])",           "tensor()", "x", "tensor(x[4])");
        checkConcat("tensor<float>(x[3])",    "tensor()", "x", "tensor<float>(x[4])");
        checkConcat("tensor<bfloat16>(x[3])", "tensor()", "x", "tensor<bfloat16>(x[4])");
        checkConcat("tensor<int8>(x[3])",     "tensor()", "x", "tensor<int8>(x[4])");
        // specific for Java
        checkConcat("tensor(x[])",            "tensor(x[2])", "x", "tensor(x[])");
        checkConcat("tensor(x[])",            "tensor(x[2])", "y", "tensor(x[],y[2])");
        checkConcat("tensor(x[3])",           "tensor(x[2])", "y", "tensor(x[2],y[2])");
        // invalid combinations must fail
        checkConcatFails("tensor(x{})",       "tensor(x[2])", "x");
        checkConcatFails("tensor(x{})",       "tensor(x{})",  "x");
        checkConcatFails("tensor(x{})",       "tensor()",     "x");
    }

    @Test
    public void verifyPeek() {
        checkPeek("tensor(x[10],y[20],z[30])", mkl("x"), "tensor(y[20],z[30])");
        checkPeek("tensor(x[10],y[20],z[30])", mkl("y"), "tensor(x[10],z[30])");
        checkPeek("tensor<float>(x[10],y[20],z[30])", mkl("z"), "tensor<float>(x[10],y[20])");
        checkPeek("tensor<bfloat16>(x[10],y[20],z[30])", mkl("z"), "tensor<bfloat16>(x[10],y[20])");
        checkPeek("tensor<int8>(x[10],y[20],z[30])", mkl("z"), "tensor<int8>(x[10],y[20])");
        checkPeek("tensor(x[10],y[20],z[30])", mkl("x", "z"), "tensor(y[20])");
        checkPeek("tensor<float>(x[10],y[20],z[30])", mkl("z", "x"), "tensor<float>(y[20])");
        checkPeek("tensor<bfloat16>(x[10],y[20],z[30])", mkl("z", "x"), "tensor<bfloat16>(y[20])");
        checkPeek("tensor<int8>(x[10],y[20],z[30])", mkl("z", "x"), "tensor<int8>(y[20])");
        checkPeek("tensor(x[10],y[20],z[30])", mkl("x", "y", "z"), "tensor()");
        checkPeek("tensor<float>(x[10],y[20],z[30])", mkl("x", "y", "z"), "tensor()");
        checkPeek("tensor<bfloat16>(x[10],y[20],z[30])", mkl("x", "y", "z"), "tensor()");
        checkPeek("tensor<int8>(x[10],y[20],z[30])", mkl("x", "y", "z"), "tensor()");
        checkPeek("tensor(x[10],y{},z[30])", mkl("x"), "tensor(y{},z[30])");
        checkPeek("tensor(x[10],y{},z[30])", mkl("y"), "tensor(x[10],z[30])");
        checkPeek("tensor<float>(x[10],y{},z[30])", mkl("z"), "tensor<float>(x[10],y{})");
        checkPeek("tensor<bfloat16>(x[10],y{},z[30])", mkl("z"), "tensor<bfloat16>(x[10],y{})");
        checkPeek("tensor<int8>(x[10],y{},z[30])", mkl("z"), "tensor<int8>(x[10],y{})");
        checkPeek("tensor(x[10],y{},z[30])", mkl("x", "z"), "tensor(y{})");
        checkPeek("tensor<float>(x[10],y{},z[30])", mkl("z", "x"), "tensor<float>(y{})");
        checkPeek("tensor<bfloat16>(x[10],y{},z[30])", mkl("z", "x"), "tensor<bfloat16>(y{})");
        checkPeek("tensor<int8>(x[10],y{},z[30])", mkl("z", "x"), "tensor<int8>(y{})");
        checkPeek("tensor(x[10],y{},z[30])", mkl("x", "y", "z"), "tensor()");
        checkPeek("tensor<float>(x[10],y{},z[30])", mkl("x", "y", "z"), "tensor()");
        checkPeek("tensor<bfloat16>(x[10],y{},z[30])", mkl("x", "y", "z"), "tensor()");
        checkPeek("tensor<int8>(x[10],y{},z[30])", mkl("x", "y", "z"), "tensor()");
        checkFullPeek("tensor(x[10],y[20],z[30])");
        checkFullPeek("tensor<float>(x[10],y[20],z[30])");
        checkFullPeek("tensor<bfloat16>(x[10],y[20],z[30])");
        checkFullPeek("tensor<int8>(x[10],y[20],z[30])");
        checkFullPeek("tensor(x[10],y{},z[30])");
        checkFullPeek("tensor<float>(x[10],y{},z[30])");
        checkFullPeek("tensor<bfloat16>(x[10],y{},z[30])");
        checkFullPeek("tensor<int8>(x[10],y{},z[30])");
        checkPeekFails("tensor()", mkl());
        checkPeekFails("tensor()", mkl("x"));
        checkPeekFails("tensor(y{})", mkl("x"));
        checkPeekFails("tensor(y{})", mkl("y", "y"));
        checkPeekFails("tensor<float>(y[10])", mkl("x"));
    }

    @Test
    public void verifyCellCast() {
        checkCast("tensor(x[10],y[20],z[30])", TensorType.Value.FLOAT, "tensor<float>(x[10],y[20],z[30])");
        checkCasts("tensor<double>(x[10])");
        checkCasts("tensor<float>(x[10])");
        checkCasts("tensor<bfloat16>(x[10])");
        checkCasts("tensor<int8>(x[10])");
        checkCasts("tensor<double>(x{})");
        checkCasts("tensor<float>(x{})");
        checkCasts("tensor<bfloat16>(x{})");
        checkCasts("tensor<int8>(x{})");
        checkCasts("tensor<double>(x{},y[5])");
        checkCasts("tensor<float>(x{},y[5])");
        checkCasts("tensor<bfloat16>(x{},y[5])");
        checkCasts("tensor<int8>(x{},y[5])");
        checkCast("tensor()", TensorType.Value.DOUBLE, "tensor()");
        checkCastFails("tensor()", TensorType.Value.FLOAT);
        checkCastFails("tensor()", TensorType.Value.BFLOAT16);
        checkCastFails("tensor()", TensorType.Value.INT8);
    }

    private static void checkMap(String specA, String expected) {
        var a = TensorType.fromSpec(specA);
        var result = TypeResolver.map(a);
        assertEquals(expected, result.toString());
    }

    private static void checkJoin(String specA, String specB, String expected) {
        var a = TensorType.fromSpec(specA);
        var b = TensorType.fromSpec(specB);
        var result = TypeResolver.join(a, b);
        assertEquals(expected, result.toString());
    }

    private static void checkJoinFails(String specA, String specB) {
        var a = TensorType.fromSpec(specA);
        var b = TensorType.fromSpec(specB);
        boolean caught = false;
        try {
            var result = TypeResolver.join(a, b);
            System.err.println("join of "+a+" and "+b+" produces: "+result);
        } catch (IllegalArgumentException e) {
            caught = true;
        }
        assertTrue(caught);
    }

    private static void checkReduce(String specA, List<String> dims, String expected) {
        var a = TensorType.fromSpec(specA);
        var result = TypeResolver.reduce(a, dims);
        assertEquals(expected, result.toString());
    }

    private static void checkFullReduce(String specA) {
        String expected = "tensor()";
        List<String> dims = new ArrayList<>();
        checkReduce(specA, dims, expected);
        var a = TensorType.fromSpec(specA);
        for (var dim : a.dimensions()) {
            dims.add(dim.name());
        }
        checkReduce(specA, dims, expected);
    }

    private static void checkReduceFails(String specA, String dim) {
        var a = TensorType.fromSpec(specA);
        boolean caught = false;
        try {
            var result = TypeResolver.reduce(a, mkl(dim));
            System.err.println("Reduce "+specA+" with dim "+dim+" produces: "+result);
        } catch (IllegalArgumentException e) {
            caught = true;
        }
        assertTrue(caught);
    }

    private static void checkMerge(String specA, String specB, String expected) {
        var a = TensorType.fromSpec(specA);
        var b = TensorType.fromSpec(specB);
        var result = TypeResolver.merge(a, b);
        assertEquals(expected, result.toString());
    }

    private static void checkMergeFails(String specA, String specB) {
        var a = TensorType.fromSpec(specA);
        var b = TensorType.fromSpec(specB);
        boolean caught = false;
        try {
            var result = TypeResolver.merge(a, b);
            System.err.println("merge of "+a+" and "+b+" produces: "+result);
        } catch (IllegalArgumentException e) {
            caught = true;
        }
        assertTrue(caught);
    }

    private static void checkRename(String specA, List<String> fromDims, List<String> toDims, String expected) {
        var a = TensorType.fromSpec(specA);
        var result = TypeResolver.rename(a, fromDims, toDims);
        assertEquals(expected, result.toString());
    }

    private static void checkRenameFails(String specA, List<String> fromDims, List<String> toDims) {
        var a = TensorType.fromSpec(specA);
        boolean caught = false;
        try {
            var result = TypeResolver.rename(a, fromDims, toDims);
            System.err.println("rename "+a+" produces: "+result);
        } catch (IllegalArgumentException e) {
            caught = true;
        }
        assertTrue(caught);
    }

    private static void checkConcat(String specA, String specB, String dim, String expected) {
        var a = TensorType.fromSpec(specA);
        var b = TensorType.fromSpec(specB);
        var result = TypeResolver.concat(a, b, dim);
        assertEquals(expected, result.toString());
    }

    private static void checkConcatFails(String specA, String specB, String dim) {
        var a = TensorType.fromSpec(specA);
        var b = TensorType.fromSpec(specB);
        boolean caught = false;
        try {
            var result = TypeResolver.concat(a, b, dim);
            System.err.println("concat "+a+" and "+b+" along "+dim+" produces: "+result);
        } catch (IllegalArgumentException e) {
            caught = true;
        }
        assertTrue(caught);
    }

    private static void checkPeek(String specA, List<String> dims, String expected) {
        var a = TensorType.fromSpec(specA);
        var result = TypeResolver.peek(a, dims);
        assertEquals(expected, result.toString());
    }

    private static void checkFullPeek(String specA) {
        String expected = "tensor()";
        List<String> dims = new ArrayList<>();
        var a = TensorType.fromSpec(specA);
        for (var dim : a.dimensions()) {
            dims.add(dim.name());
        }
        checkPeek(specA, dims, expected);
    }

    private static void checkPeekFails(String specA, List<String> dims) {
        var a = TensorType.fromSpec(specA);
        boolean caught = false;
        try {
            var result = TypeResolver.peek(a, dims);
            System.err.println("Peek "+specA+" with dims "+dims+" produces: "+result);
        } catch (IllegalArgumentException e) {
            caught = true;
        }
        assertTrue(caught);
    }

    private static void checkCast(String specA, TensorType.Value newValueType, String expected) {
        var a = TensorType.fromSpec(specA);
        var result = TypeResolver.cell_cast(a, newValueType);
        assertEquals(expected, result.toString());
    }

    private static void checkCasts(String specA) {
        var a = TensorType.fromSpec(specA);
        for (var newValueType : TensorType.Value.values()) {
            var result = TypeResolver.cell_cast(a, newValueType);
            assertEquals(result.valueType(), newValueType);
            assertEquals(result.dimensions(), a.dimensions());
        }
    }

    private static void checkCastFails(String specA, TensorType.Value newValueType) {
        var a = TensorType.fromSpec(specA);
        boolean caught = false;
        try {
            var result = TypeResolver.cell_cast(a, newValueType);
            System.err.println("cast of "+a+" to "+newValueType+" produces: "+result);
        } catch (IllegalArgumentException e) {
            caught = true;
        }
        assertTrue(caught);
    }

}