@@ -427,3 +427,130 @@ fn test_float_ops() {
427427 assert ! ( abs( t. mul_towards_zero( 2.0 ) . simplify( ) . as_f64( ) - 20.0 ) < 0.1 ) ;
428428 assert ! ( abs( t. div_towards_zero( 2.0 ) . simplify( ) . as_f64( ) - 5.0 ) < 0.1 ) ;
429429}
430+
431+ #[ test]
432+ fn test_int_sum ( ) {
433+ // Test Sum for owned Int values
434+ let ints = vec ! [
435+ Int :: from_i64( 1 ) ,
436+ Int :: from_i64( 2 ) ,
437+ Int :: from_i64( 3 ) ,
438+ Int :: from_i64( 4 ) ,
439+ Int :: from_i64( 5 ) ,
440+ ] ;
441+ let sum: Int = ints. into_iter ( ) . sum ( ) ;
442+ assert_eq ! ( sum. simplify( ) , 15 ) ;
443+
444+ // Test Sum for borrowed Int values
445+ let ints = [ Int :: from_i64 ( 10 ) , Int :: from_i64 ( 20 ) , Int :: from_i64 ( 30 ) ] ;
446+ let sum: Int = ints. iter ( ) . sum ( ) ;
447+ assert_eq ! ( sum. simplify( ) , 60 ) ;
448+
449+ // Test empty iterator gives zero
450+ let empty: Vec < Int > = vec ! [ ] ;
451+ let sum: Int = empty. into_iter ( ) . sum ( ) ;
452+ assert_eq ! ( sum, 0 ) ;
453+
454+ // Test with symbolic values
455+ let x = Int :: new_const ( "x" ) ;
456+ let y = Int :: new_const ( "y" ) ;
457+ let z = Int :: new_const ( "z" ) ;
458+ let ints = vec ! [ x. clone( ) , y. clone( ) , z. clone( ) ] ;
459+ let sum: Int = ints. into_iter ( ) . sum ( ) ;
460+ // sum should be x + y + z
461+ let expected = & ( & x + & y) + & z;
462+ assert_eq ! ( sum, expected) ;
463+ }
464+
465+ #[ test]
466+ fn test_int_product ( ) {
467+ // Test Product for owned Int values
468+ let ints = vec ! [ Int :: from_i64( 2 ) , Int :: from_i64( 3 ) , Int :: from_i64( 4 ) ] ;
469+ let product: Int = ints. into_iter ( ) . product ( ) ;
470+ assert_eq ! ( product. simplify( ) , 24 ) ;
471+
472+ // Test Product for borrowed Int values
473+ let ints = [ Int :: from_i64 ( 5 ) , Int :: from_i64 ( 6 ) ] ;
474+ let product: Int = ints. iter ( ) . product ( ) ;
475+ assert_eq ! ( product. simplify( ) , 30 ) ;
476+
477+ // Test empty iterator gives one
478+ let empty: Vec < Int > = vec ! [ ] ;
479+ let product: Int = empty. into_iter ( ) . product ( ) ;
480+ assert_eq ! ( product, 1 ) ;
481+
482+ // Test with symbolic values
483+ let x = Int :: new_const ( "x" ) ;
484+ let y = Int :: new_const ( "y" ) ;
485+ let ints = vec ! [ x. clone( ) , y. clone( ) , Int :: from_i64( 2 ) ] ;
486+ let product: Int = ints. into_iter ( ) . product ( ) ;
487+ // product should be x * y * 2
488+ let expected = & ( & x * & y) * 2 ;
489+ assert_eq ! ( product, expected) ;
490+ }
491+
492+ #[ test]
493+ fn test_real_sum ( ) {
494+ // Test Sum for owned Real values
495+ let reals = vec ! [
496+ Real :: from_rational( 1 , 2 ) ,
497+ Real :: from_rational( 1 , 3 ) ,
498+ Real :: from_rational( 1 , 6 ) ,
499+ ] ;
500+ let sum: Real = reals. into_iter ( ) . sum ( ) ;
501+ assert_eq ! ( sum. simplify( ) , Real :: from_rational( 1 , 1 ) ) ;
502+
503+ // Test Sum for borrowed Real values
504+ let reals = [
505+ Real :: from_rational ( 2 , 1 ) ,
506+ Real :: from_rational ( 3 , 1 ) ,
507+ Real :: from_rational ( 5 , 1 ) ,
508+ ] ;
509+ let sum: Real = reals. iter ( ) . sum ( ) ;
510+ assert_eq ! ( sum. simplify( ) , Real :: from_rational( 10 , 1 ) ) ;
511+
512+ // Test empty iterator gives zero
513+ let empty: Vec < Real > = vec ! [ ] ;
514+ let sum: Real = empty. into_iter ( ) . sum ( ) ;
515+ assert_eq ! ( sum, Real :: from_rational( 0 , 1 ) ) ;
516+
517+ // Test with symbolic values
518+ let x = Real :: new_const ( "x" ) ;
519+ let y = Real :: new_const ( "y" ) ;
520+ let reals = vec ! [ x. clone( ) , y. clone( ) , Real :: from_rational( 1 , 1 ) ] ;
521+ let sum: Real = reals. into_iter ( ) . sum ( ) ;
522+ // sum should be x + y + 1
523+ let expected = & ( & x + & y) + & Real :: from_rational ( 1 , 1 ) ;
524+ assert_eq ! ( sum, expected) ;
525+ }
526+
527+ #[ test]
528+ fn test_real_product ( ) {
529+ // Test Product for owned Real values
530+ let reals = vec ! [
531+ Real :: from_rational( 2 , 1 ) ,
532+ Real :: from_rational( 3 , 1 ) ,
533+ Real :: from_rational( 4 , 1 ) ,
534+ ] ;
535+ let product: Real = reals. into_iter ( ) . product ( ) ;
536+ assert_eq ! ( product. simplify( ) , Real :: from_rational( 24 , 1 ) ) ;
537+
538+ // Test Product for borrowed Real values
539+ let reals = [ Real :: from_rational ( 1 , 2 ) , Real :: from_rational ( 1 , 3 ) ] ;
540+ let product: Real = reals. iter ( ) . product ( ) ;
541+ assert_eq ! ( product. simplify( ) , Real :: from_rational( 1 , 6 ) ) ;
542+
543+ // Test empty iterator gives one
544+ let empty: Vec < Real > = vec ! [ ] ;
545+ let product: Real = empty. into_iter ( ) . product ( ) ;
546+ assert_eq ! ( product, Real :: from_rational( 1 , 1 ) ) ;
547+
548+ // Test with symbolic values
549+ let x = Real :: new_const ( "x" ) ;
550+ let y = Real :: new_const ( "y" ) ;
551+ let reals = vec ! [ x. clone( ) , y. clone( ) , Real :: from_rational( 2 , 1 ) ] ;
552+ let product: Real = reals. into_iter ( ) . product ( ) ;
553+ // product should be x * y * 2
554+ let expected = & ( & x * & y) * & Real :: from_rational ( 2 , 1 ) ;
555+ assert_eq ! ( product, expected) ;
556+ }
0 commit comments