MCPcopy Create free account
hub / github.com/benbjohnson/immutable / TestSortedMap_Set

Function TestSortedMap_Set

immutable_test.go:1809–1893  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1807}
1808
1809func TestSortedMap_Set(t *testing.T) {
1810 t.Run("Simple", func(t *testing.T) {
1811 m := NewSortedMap[int, string](nil)
1812 m = m.Set(100, "foo")
1813 if v, ok := m.Get(100); !ok || v != "foo" {
1814 t.Fatalf("unexpected value: <%v,%v>", v, ok)
1815 } else if got, exp := m.Len(), 1; got != exp {
1816 t.Fatalf("SortedMap.Len()=%d, exp %d", got, exp)
1817 }
1818 })
1819
1820 t.Run("Small", func(t *testing.T) {
1821 const n = 1000
1822 m := NewSortedMap[int, int](nil)
1823 for i := 0; i < n; i++ {
1824 m = m.Set(i, i+1)
1825 }
1826 for i := 0; i < n; i++ {
1827 if v, ok := m.Get(i); !ok || v != i+1 {
1828 t.Fatalf("unexpected value for key=%v: <%v,%v>", i, v, ok)
1829 }
1830 }
1831 })
1832
1833 t.Run("Large", func(t *testing.T) {
1834 if testing.Short() {
1835 t.Skip("skipping: short")
1836 }
1837
1838 const n = 1000000
1839 m := NewSortedMap[int, int](nil)
1840 for i := 0; i < n; i++ {
1841 m = m.Set(i, i+1)
1842 }
1843 for i := 0; i < n; i++ {
1844 if v, ok := m.Get(i); !ok || v != i+1 {
1845 t.Fatalf("unexpected value for key=%v: <%v,%v>", i, v, ok)
1846 }
1847 }
1848 })
1849
1850 t.Run("StringKeys", func(t *testing.T) {
1851 m := NewSortedMap[string, string](nil)
1852 m = m.Set("foo", "bar")
1853 m = m.Set("baz", "bat")
1854 m = m.Set("", "EMPTY")
1855 if v, ok := m.Get("foo"); !ok || v != "bar" {
1856 t.Fatalf("unexpected value: <%v,%v>", v, ok)
1857 } else if v, ok := m.Get("baz"); !ok || v != "bat" {
1858 t.Fatalf("unexpected value: <%v,%v>", v, ok)
1859 } else if v, ok := m.Get(""); !ok || v != "EMPTY" {
1860 t.Fatalf("unexpected value: <%v,%v>", v, ok)
1861 }
1862 if v, ok := m.Get("no_such_key"); ok {
1863 t.Fatalf("expected no value: <%v,%v>", v, ok)
1864 }
1865 })
1866

Callers

nothing calls this directly

Calls 8

SetMethod · 0.95
ExistingKeyMethod · 0.95
NewKeyMethod · 0.95
ValidateMethod · 0.95
RunRandomFunction · 0.85
NewTSortedMapFunction · 0.85
GetMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…