@@ -479,6 +479,68 @@ func TestFnv32(t *testing.T) {
479479
480480}
481481
482+ func TestUpdate (t * testing.T ) {
483+ m := New [Animal ]()
484+ lion := Animal {"lion" }
485+
486+ m .Set ("safari" , lion )
487+ m .Update ("safari" , func (exists bool , valueInMap Animal ) (Animal , bool ) {
488+ if ! exists {
489+ t .Error ("Update recieved false exists flag for existing key" )
490+ }
491+ valueInMap .name = "tiger"
492+ return valueInMap , true
493+ })
494+ safari , ok := m .Get ("safari" )
495+ if safari .name != "tiger" || ! ok {
496+ t .Error ("Set, then Update failed" )
497+ }
498+
499+ m .Update ("marine" , func (exists bool , valueInMap Animal ) (Animal , bool ) {
500+ if exists {
501+ t .Error ("Update recieved exists flag for empty key" )
502+ }
503+ if valueInMap .name != "" {
504+ t .Error ("Update did not receive zero value for non existing key" )
505+ }
506+ valueInMap .name = "whale"
507+ return valueInMap , true
508+ })
509+ marineAnimals , ok := m .Get ("marine" )
510+ if marineAnimals .name != "whale" || ! ok {
511+ t .Error ("Update on non-existing key failed" )
512+ }
513+
514+ // return false to prevent updateing map
515+ m .Set ("safari" , lion )
516+ m .Update ("safari" , func (exists bool , valueInMap Animal ) (Animal , bool ) {
517+ if ! exists {
518+ t .Error ("Update recieved false exists flag for existing key" )
519+ }
520+ valueInMap .name = "tiger"
521+ return valueInMap , false
522+ })
523+ safari , ok = m .Get ("safari" )
524+ if safari .name != "lion" || ! ok {
525+ t .Error ("Set, then aborting Update failed" )
526+ }
527+
528+ m .Update ("tundra" , func (exists bool , valueInMap Animal ) (Animal , bool ) {
529+ if exists {
530+ t .Error ("Update recieved exists flag for empty key" )
531+ }
532+ if valueInMap .name != "" {
533+ t .Error ("Update did not receive zero value for non existing key" )
534+ }
535+ valueInMap .name = "moose"
536+ return valueInMap , false
537+ })
538+ _ , ok = m .Get ("tundra" )
539+ if ok {
540+ t .Error ("Update aborting on non-existing key failed" )
541+ }
542+ }
543+
482544func TestUpsert (t * testing.T ) {
483545 dolphin := Animal {"dolphin" }
484546 whale := Animal {"whale" }
0 commit comments