@@ -420,6 +420,48 @@ TEST(TensorUtilTest, DoubleAndInfinitNanElementsAreCloseAndEqual) {
420
420
EXPECT_TENSORS_CLOSE_AND_EQUAL (a, b);
421
421
}
422
422
423
+ // Testing closeness with tolerances
424
+
425
+ TEST (TensorUtilTest, TensorsAreCloseWithTol) {
426
+ TensorFactory<ScalarType::Float> tf;
427
+ TensorFactory<ScalarType::Double> td;
428
+
429
+ // Create two tensors with identical shape and dtype, but different data.
430
+ Tensor af = tf.make (/* sizes=*/ {2 , 2 }, /* data=*/ {1.0 , 2.099999 , 0.0 , -0.05 });
431
+ Tensor bf = tf.make (/* sizes=*/ {2 , 2 }, /* data=*/ {1.099999 , 2.0 , 0.05 , 0.0 });
432
+
433
+ EXPECT_TENSOR_CLOSE_WITH_TOL (af, bf, 0.0 , 0.1 );
434
+
435
+ // Create two tensors with identical shape and dtype, but different data.
436
+ Tensor ad = td.make (/* sizes=*/ {2 , 2 }, /* data=*/ {1.099 , 2.199 , NAN, -9.0 });
437
+ Tensor bd = td.make (/* sizes=*/ {2 , 2 }, /* data=*/ {1.0 , 2.0 , NAN, -10.0 });
438
+
439
+ EXPECT_TENSOR_CLOSE_WITH_TOL (ad, bd, 0.1 , 0.0 );
440
+ }
441
+
442
+ TEST (TensorUtilTest, TensorsAreNotCloseWithTol) {
443
+ TensorFactory<ScalarType::Float> tf;
444
+ TensorFactory<ScalarType::Double> td;
445
+
446
+ // Create two tensors with identical shape and dtype, but different data.
447
+ Tensor af = tf.make (/* sizes=*/ {3 }, /* data=*/ {1.00 , NAN, -10.0 });
448
+ Tensor bf = tf.make (/* sizes=*/ {3 }, /* data=*/ {1.11 , NAN, -10.0 });
449
+
450
+ EXPECT_TENSOR_NOT_CLOSE_WITH_TOL (af, bf, 0.0 , 0.1 );
451
+
452
+ // Create two tensors with identical shape and dtype, but different data.
453
+ Tensor ad = td.make (/* sizes=*/ {3 }, /* data=*/ {1.0 , 0.0 , -10.0 });
454
+ Tensor bd = td.make (/* sizes=*/ {3 }, /* data=*/ {1.0 , 0.0 , -9.0 });
455
+
456
+ EXPECT_TENSOR_NOT_CLOSE_WITH_TOL (ad, bd, 0.1 , 0.0 );
457
+
458
+ // Create two tensors with identical shape and dtype, but different data.
459
+ ad = tf.make (/* sizes=*/ {3 }, /* data=*/ {1.0 , 2.0 , 0.00001 });
460
+ bd = tf.make (/* sizes=*/ {3 }, /* data=*/ {1.0 , 2.0 , 0.0 });
461
+
462
+ EXPECT_TENSOR_NOT_CLOSE_WITH_TOL (ad, bd, 0.1 , 0.0 );
463
+ }
464
+
423
465
//
424
466
// Tests for shape-agnostic data equality.
425
467
//
@@ -585,6 +627,48 @@ TEST(TensorUtilTest, TensorDataMismatched) {
585
627
EXPECT_TENSORS_DATA_NOT_CLOSE_OR_EQUAL (t_zero_dim, t_empty);
586
628
}
587
629
630
+ // Testing data closeness with tolerances
631
+
632
+ TEST (TensorUtilTest, TensorDataCloseWithTol) {
633
+ TensorFactory<ScalarType::Float> tf;
634
+ TensorFactory<ScalarType::Double> td;
635
+
636
+ // Create two tensors with identical shape and dtype, but different data.
637
+ Tensor af = tf.make (/* sizes=*/ {4 , 1 }, /* data=*/ {1.0 , 2.099 , 0.0 , -0.05 });
638
+ Tensor bf = tf.make (/* sizes=*/ {2 , 2 }, /* data=*/ {1.099 , 2.0 , 0.05 , 0.0 });
639
+
640
+ EXPECT_TENSOR_DATA_CLOSE_WITH_TOL (af, bf, 0.0 , 0.1 );
641
+
642
+ // Create two tensors with identical shape and dtype, but different data.
643
+ Tensor ad = td.make (/* sizes=*/ {2 , 2 }, /* data=*/ {1.099 , 2.199 , NAN, -9.0 });
644
+ Tensor bd = td.make (/* sizes=*/ {4 }, /* data=*/ {1.0 , 2.0 , NAN, -10.0 });
645
+
646
+ EXPECT_TENSOR_DATA_CLOSE_WITH_TOL (ad, bd, 0.1 , 0.0 );
647
+ }
648
+
649
+ TEST (TensorUtilTest, TensorDataNotCloseWithTol) {
650
+ TensorFactory<ScalarType::Float> tf;
651
+ TensorFactory<ScalarType::Double> td;
652
+
653
+ // Create two tensors with identical shape and dtype, but different data.
654
+ Tensor af = tf.make (/* sizes=*/ {3 }, /* data=*/ {1.00 , 0.0 , -10.0 });
655
+ Tensor bf = tf.make (/* sizes=*/ {3 , 1 }, /* data=*/ {1.11 , 0.0 , -10.0 });
656
+
657
+ EXPECT_TENSOR_DATA_NOT_CLOSE_WITH_TOL (af, bf, 0.0 , 0.1 );
658
+
659
+ // Create two tensors with identical shape and dtype, but different data.
660
+ Tensor ad = td.make (/* sizes=*/ {2 , 2 }, /* data=*/ {1.0 , 0.0 , -10.0 , 0.0 });
661
+ Tensor bd = td.make (/* sizes=*/ {4 }, /* data=*/ {1.0 , 0.0 , -9.0 , 0.0 });
662
+
663
+ EXPECT_TENSOR_DATA_NOT_CLOSE_WITH_TOL (ad, bd, 0.1 , 0.0 );
664
+
665
+ // Create two tensors with identical shape and dtype, but different data.
666
+ ad = tf.make (/* sizes=*/ {1 , 4 }, /* data=*/ {1.0 , 2.0 , NAN, 0.00001 });
667
+ bd = tf.make (/* sizes=*/ {2 , 2 }, /* data=*/ {1.0 , 2.0 , NAN, 0.0 });
668
+
669
+ EXPECT_TENSOR_DATA_NOT_CLOSE_WITH_TOL (ad, bd, 0.1 , 0.0 );
670
+ }
671
+
588
672
//
589
673
// Tests for TensorList helpers.
590
674
//
0 commit comments