goki.dev/laser@v0.1.34/maps_test.go (about)

     1  // Copyright (c) 2023, The Goki Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package laser
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestCopyMap(t *testing.T) {
    12  	var tof map[string]float32
    13  	var tos map[string]string
    14  	fmf := map[string]float32{"a": 1, "b": 2, "c": 3}
    15  	fms := map[string]string{"a": "3", "b": "4", "c": "5"}
    16  	err := CopyMapRobust(&tof, fmf)
    17  	if err != nil {
    18  		t.Errorf("copy float: %s\n", err.Error())
    19  	}
    20  	for i := range fmf {
    21  		if tof[i] != fmf[i] {
    22  			t.Errorf("tof %s: %g != %g\n", i, tof[i], fmf[i])
    23  		}
    24  	}
    25  	err = CopyMapRobust(&tos, fms)
    26  	if err != nil {
    27  		t.Errorf("copy string: %s\n", err.Error())
    28  	}
    29  	for i := range fms {
    30  		if tos[i] != fms[i] {
    31  			t.Errorf("tos %s: %s != %s\n", i, tos[i], fms[i])
    32  		}
    33  	}
    34  	err = CopyMapRobust(&tof, fms)
    35  	if err != nil {
    36  		t.Errorf("copy float = string: %s\n", err.Error())
    37  	}
    38  	for i := range fms {
    39  		if ToString(tof[i]) != fms[i] {
    40  			t.Errorf("tof %s: %g != %s\n", i, tof[i], fms[i])
    41  		}
    42  	}
    43  	delete(fms, "b")
    44  	err = CopyMapRobust(&tof, fms)
    45  	if err != nil {
    46  		t.Errorf("copy float = string: %s\n", err.Error())
    47  	}
    48  	if len(tof) != len(fms) {
    49  		t.Errorf("copy float = string: size not 2: %d\n", len(tof))
    50  	}
    51  	for i := range fms {
    52  		if ToString(tof[i]) != fms[i] {
    53  			t.Errorf("tof %s: %g != %s\n", i, tof[i], fms[i])
    54  		}
    55  	}
    56  	fms["e"] = "7"
    57  	err = CopyMapRobust(&tof, fms)
    58  	if err != nil {
    59  		t.Errorf("copy float = string: %s\n", err.Error())
    60  	}
    61  	if len(tof) != len(fms) {
    62  		t.Errorf("copy float = string: size not 3: %d\n", len(tof))
    63  	}
    64  	for i := range fms {
    65  		if ToString(tof[i]) != fms[i] {
    66  			t.Errorf("tof %s: %g != %s\n", i, tof[i], fms[i])
    67  		}
    68  	}
    69  
    70  	var toc map[string]map[string]float32
    71  	fmc := map[string]map[string]string{"q": {"a": "1", "b": "2"}, "z": {"c": "3", "d": "4"}}
    72  	err = CopyMapRobust(&toc, fmc)
    73  	if err != nil {
    74  		t.Errorf("copy map[string]map[string]float = map[string]map[string]string: %s\n", err.Error())
    75  	}
    76  	for i := range fmc {
    77  		fmci := fmc[i]
    78  		toci := toc[i]
    79  		for j := range fmci {
    80  			if ToString(toci[j]) != fmci[j] {
    81  				t.Errorf("toci,j %s,%s: %g != %s\n", i, j, toci[j], fmci[j])
    82  			}
    83  		}
    84  	}
    85  }
    86  
    87  // test map type functions
    88  func TestMapType(t *testing.T) {
    89  	var mp map[string]int
    90  
    91  	ts := MapValueType(mp).String()
    92  	if ts != "int" {
    93  		t.Errorf("map val type should be int, not: %v\n", ts)
    94  	}
    95  
    96  	ts = MapValueType(&mp).String()
    97  	if ts != "int" {
    98  		t.Errorf("map val type should be int, not: %v\n", ts)
    99  	}
   100  
   101  	ts = MapKeyType(mp).String()
   102  	if ts != "string" {
   103  		t.Errorf("map key type should be string, not: %v\n", ts)
   104  	}
   105  
   106  	ts = MapKeyType(&mp).String()
   107  	if ts != "string" {
   108  		t.Errorf("map key type should be string, not: %v\n", ts)
   109  	}
   110  
   111  }