@@ -1242,6 +1242,118 @@ func Test_PopUnsafe(t *testing.T) {
1242
1242
}
1243
1243
}
1244
1244
1245
+ func Test_PopNSafe (t * testing.T ) {
1246
+ a := NewSet [string ]()
1247
+ a .Add ("a" )
1248
+ a .Add ("b" )
1249
+ a .Add ("c" )
1250
+ a .Add ("d" )
1251
+
1252
+ // Test pop with n <= 0
1253
+ items , count := a .PopN (0 )
1254
+ if count != 0 {
1255
+ t .Errorf ("expected 0 items popped, got %d" , count )
1256
+ }
1257
+ items , count = a .PopN (- 1 )
1258
+ if count != 0 {
1259
+ t .Errorf ("expected 0 items popped, got %d" , count )
1260
+ }
1261
+
1262
+ captureSet := NewSet [string ]()
1263
+
1264
+ // Test pop 2 items
1265
+ items , count = a .PopN (2 )
1266
+ if count != 2 {
1267
+ t .Errorf ("expected 2 items popped, got %d" , count )
1268
+ }
1269
+ if len (items ) != 2 {
1270
+ t .Errorf ("expected 2 items in slice, got %d" , len (items ))
1271
+ }
1272
+ if a .Cardinality () != 2 {
1273
+ t .Errorf ("expected 2 items remaining, got %d" , a .Cardinality ())
1274
+ }
1275
+ captureSet .Append (items ... )
1276
+
1277
+ // Test pop more than remaining
1278
+ items , count = a .PopN (3 )
1279
+ if count != 2 {
1280
+ t .Errorf ("expected 2 items popped, got %d" , count )
1281
+ }
1282
+ if a .Cardinality () != 0 {
1283
+ t .Errorf ("expected 0 items remaining, got %d" , a .Cardinality ())
1284
+ }
1285
+ captureSet .Append (items ... )
1286
+
1287
+ // Test pop from empty set
1288
+ items , count = a .PopN (1 )
1289
+ if count != 0 {
1290
+ t .Errorf ("expected 0 items popped, got %d" , count )
1291
+ }
1292
+ if len (items ) != 0 {
1293
+ t .Errorf ("expected empty slice, got %d items" , len (items ))
1294
+ }
1295
+
1296
+ if ! captureSet .Contains ("c" , "a" , "d" , "b" ) {
1297
+ t .Error ("unexpected result set; should be a,b,c,d (any order is fine" )
1298
+ }
1299
+ }
1300
+
1301
+ func Test_PopNUnsafe (t * testing.T ) {
1302
+ a := NewThreadUnsafeSet [string ]()
1303
+ a .Add ("a" )
1304
+ a .Add ("b" )
1305
+ a .Add ("c" )
1306
+ a .Add ("d" )
1307
+
1308
+ // Test pop with n <= 0
1309
+ items , count := a .PopN (0 )
1310
+ if count != 0 {
1311
+ t .Errorf ("expected 0 items popped, got %d" , count )
1312
+ }
1313
+ items , count = a .PopN (- 1 )
1314
+ if count != 0 {
1315
+ t .Errorf ("expected 0 items popped, got %d" , count )
1316
+ }
1317
+
1318
+ captureSet := NewThreadUnsafeSet [string ]()
1319
+
1320
+ // Test pop 2 items
1321
+ items , count = a .PopN (2 )
1322
+ if count != 2 {
1323
+ t .Errorf ("expected 2 items popped, got %d" , count )
1324
+ }
1325
+ if len (items ) != 2 {
1326
+ t .Errorf ("expected 2 items in slice, got %d" , len (items ))
1327
+ }
1328
+ if a .Cardinality () != 2 {
1329
+ t .Errorf ("expected 2 items remaining, got %d" , a .Cardinality ())
1330
+ }
1331
+ captureSet .Append (items ... )
1332
+
1333
+ // Test pop more than remaining
1334
+ items , count = a .PopN (3 )
1335
+ if count != 2 {
1336
+ t .Errorf ("expected 2 items popped, got %d" , count )
1337
+ }
1338
+ if a .Cardinality () != 0 {
1339
+ t .Errorf ("expected 0 items remaining, got %d" , a .Cardinality ())
1340
+ }
1341
+ captureSet .Append (items ... )
1342
+
1343
+ // Test pop from empty set
1344
+ items , count = a .PopN (1 )
1345
+ if count != 0 {
1346
+ t .Errorf ("expected 0 items popped, got %d" , count )
1347
+ }
1348
+ if len (items ) != 0 {
1349
+ t .Errorf ("expected empty slice, got %d items" , len (items ))
1350
+ }
1351
+
1352
+ if ! captureSet .Contains ("c" , "a" , "d" , "b" ) {
1353
+ t .Error ("unexpected result set; should be a,b,c,d (any order is fine" )
1354
+ }
1355
+ }
1356
+
1245
1357
func Test_EmptySetProperties (t * testing.T ) {
1246
1358
empty := NewSet [string ]()
1247
1359
0 commit comments