@@ -1201,29 +1201,51 @@ func getMapFoldCases(builder baseMapCollIntBuilder) []baseMapTestCase {
12011201 name : "Fold() on empty collection" ,
12021202 coll : builder .Empty (),
12031203 args : baseMapIntArgs {
1204- reducer : func (acc Pair [int , int ], i int , current Pair [int , int ]) Pair [int , int ] {
1204+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
12051205 return acc
12061206 },
12071207 initial : nil ,
12081208 },
12091209 want1 : nil ,
12101210 },
1211+ {
1212+ name : "Fold() on empty collection with initial" ,
1213+ coll : builder .Empty (),
1214+ args : baseMapIntArgs {
1215+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
1216+ return acc
1217+ },
1218+ initial : NewPair (10 , 100 ),
1219+ },
1220+ want1 : NewPair (10 , 100 ),
1221+ },
12111222 {
12121223 name : "Fold() on one-item collection" ,
12131224 coll : builder .One (),
12141225 args : baseMapIntArgs {
1215- reducer : func (acc Pair [int , int ], i int , current Pair [int , int ]) Pair [int , int ] {
1226+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
12161227 return current
12171228 },
12181229 initial : nil ,
12191230 },
12201231 want1 : NewPair (1 , 111 ),
12211232 },
1233+ {
1234+ name : "Fold() on one-item collection with non-nil initial" ,
1235+ coll : builder .One (),
1236+ args : baseMapIntArgs {
1237+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
1238+ return NewPair (acc .Key ()+ current .Key (), acc .Val ()+ current .Val ())
1239+ },
1240+ initial : NewPair (10 , 100 ),
1241+ },
1242+ want1 : NewPair (11 , 211 ),
1243+ },
12221244 {
12231245 name : "Fold() on three-item collection" ,
12241246 coll : builder .Three (),
12251247 args : baseMapIntArgs {
1226- reducer : func (acc Pair [int , int ], i int , current Pair [int , int ]) Pair [int , int ] {
1248+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
12271249 if acc == nil {
12281250 return current
12291251 }
@@ -1233,11 +1255,24 @@ func getMapFoldCases(builder baseMapCollIntBuilder) []baseMapTestCase {
12331255 },
12341256 want1 : NewPair (6 , 666 ),
12351257 },
1258+ {
1259+ name : "Fold() on three-item collection with non-nil initial" ,
1260+ coll : builder .Three (),
1261+ args : baseMapIntArgs {
1262+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
1263+ newKey := acc .Key () + current .Key ()
1264+ newVal := acc .Val ()* 10 + current .Val ()
1265+ return NewPair (newKey , newVal )
1266+ },
1267+ initial : NewPair (10 , 100 ),
1268+ },
1269+ want1 : NewPair (16 , 113653 ),
1270+ },
12361271 {
12371272 name : "Fold() on six-item collection" ,
12381273 coll : builder .SixWithDuplicates (),
12391274 args : baseMapIntArgs {
1240- reducer : func (acc Pair [int , int ], i int , current Pair [int , int ]) Pair [int , int ] {
1275+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
12411276 return NewPair (acc .Key ()+ current .Key (), acc .Val ()+ current .Val ())
12421277 },
12431278 initial : NewPair (0 , 0 ),
@@ -1259,6 +1294,105 @@ func testMapFold(t *testing.T, builder baseMapCollIntBuilder) {
12591294 }
12601295}
12611296
1297+ func getMapFoldRevCases (builder baseMapCollIntBuilder ) []baseMapTestCase {
1298+ return []baseMapTestCase {
1299+ {
1300+ name : "Fold() on empty collection" ,
1301+ coll : builder .Empty (),
1302+ args : baseMapIntArgs {
1303+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
1304+ return acc
1305+ },
1306+ initial : nil ,
1307+ },
1308+ want1 : nil ,
1309+ },
1310+ {
1311+ name : "Fold() on empty collection with initial" ,
1312+ coll : builder .Empty (),
1313+ args : baseMapIntArgs {
1314+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
1315+ return acc
1316+ },
1317+ initial : NewPair (10 , 100 ),
1318+ },
1319+ want1 : NewPair (10 , 100 ),
1320+ },
1321+ {
1322+ name : "Fold() on one-item collection" ,
1323+ coll : builder .One (),
1324+ args : baseMapIntArgs {
1325+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
1326+ return current
1327+ },
1328+ initial : nil ,
1329+ },
1330+ want1 : NewPair (1 , 111 ),
1331+ },
1332+ {
1333+ name : "Fold() on one-item collection with non-nil initial" ,
1334+ coll : builder .One (),
1335+ args : baseMapIntArgs {
1336+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
1337+ return NewPair (acc .Key ()+ current .Key (), acc .Val ()+ current .Val ())
1338+ },
1339+ initial : NewPair (10 , 100 ),
1340+ },
1341+ want1 : NewPair (11 , 211 ),
1342+ },
1343+ {
1344+ name : "Fold() on three-item collection" ,
1345+ coll : builder .Three (),
1346+ args : baseMapIntArgs {
1347+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
1348+ if acc == nil {
1349+ return current
1350+ }
1351+ return NewPair (acc .Key ()+ current .Key (), acc .Val ()+ current .Val ())
1352+ },
1353+ initial : nil ,
1354+ },
1355+ want1 : NewPair (6 , 666 ),
1356+ },
1357+ {
1358+ name : "Fold() on three-item collection with non-nil initial" ,
1359+ coll : builder .Three (),
1360+ args : baseMapIntArgs {
1361+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
1362+ newKey := acc .Key () + current .Key ()
1363+ newVal := acc .Val ()* 10 + current .Val ()
1364+ return NewPair (newKey , newVal )
1365+ },
1366+ initial : NewPair (10 , 100 ),
1367+ },
1368+ want1 : NewPair (16 , 135631 ),
1369+ },
1370+ {
1371+ name : "Fold() on six-item collection" ,
1372+ coll : builder .SixWithDuplicates (),
1373+ args : baseMapIntArgs {
1374+ reducer : func (acc Pair [int , int ], _ int , current Pair [int , int ]) Pair [int , int ] {
1375+ return NewPair (acc .Key ()+ current .Key (), acc .Val ()+ current .Val ())
1376+ },
1377+ initial : NewPair (0 , 0 ),
1378+ },
1379+ want1 : NewPair (21 , 1332 ),
1380+ },
1381+ }
1382+ }
1383+
1384+ func testMapFoldRev (t * testing.T , builder baseMapCollIntBuilder ) {
1385+ cases := getMapFoldRevCases (builder )
1386+ for _ , tt := range cases {
1387+ t .Run (tt .name , func (t * testing.T ) {
1388+ got := tt .coll .FoldRev (tt .args .reducer , tt .args .initial )
1389+ if ! reflect .DeepEqual (got , tt .want1 ) {
1390+ t .Errorf ("FoldRev() = %v, want1 = %v" , got , tt .want1 )
1391+ }
1392+ })
1393+ }
1394+ }
1395+
12621396func getMapGetCases (builder baseMapCollIntBuilder ) []baseMapTestCase {
12631397 return []baseMapTestCase {
12641398 {
0 commit comments