From a0dba0f8aefad9a843b59040857f4a04021c54e1 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 23 Apr 2025 16:05:57 -0700 Subject: [PATCH] default slice values --- fs/ggml/ggml.go | 8 +++--- fs/ggml/ggml_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/fs/ggml/ggml.go b/fs/ggml/ggml.go index 4a1e185d2..015b2798e 100644 --- a/fs/ggml/ggml.go +++ b/fs/ggml/ggml.go @@ -105,19 +105,19 @@ func (kv KV) Bool(key string, defaultValue ...bool) bool { } func (kv KV) Strings(key string, defaultValue ...[]string) []string { - return keyValue(kv, key, &array[string]{}).values + return keyValue(kv, key, &array[string]{values: append(defaultValue, []string(nil))[0]}).values } func (kv KV) Ints(key string, defaultValue ...[]int32) []int32 { - return keyValue(kv, key, &array[int32]{}).values + return keyValue(kv, key, &array[int32]{values: append(defaultValue, []int32(nil))[0]}).values } func (kv KV) Uints(key string, defaultValue ...[]uint32) []uint32 { - return keyValue(kv, key, &array[uint32]{}).values + return keyValue(kv, key, &array[uint32]{values: append(defaultValue, []uint32(nil))[0]}).values } func (kv KV) Floats(key string, defaultValue ...[]float32) []float32 { - return keyValue(kv, key, &array[float32]{}).values + return keyValue(kv, key, &array[float32]{values: append(defaultValue, []float32(nil))[0]}).values } func (kv KV) OllamaEngineRequired() bool { diff --git a/fs/ggml/ggml_test.go b/fs/ggml/ggml_test.go index 324e40fab..c1c1b43b0 100644 --- a/fs/ggml/ggml_test.go +++ b/fs/ggml/ggml_test.go @@ -2,6 +2,7 @@ package ggml import ( "maps" + "math" "slices" "strconv" "strings" @@ -210,3 +211,61 @@ func TestTensorTypes(t *testing.T) { }) } } + +func TestKeyValue(t *testing.T) { + kv := KV{ + "general.architecture": "test", + "test.strings": &array[string]{size: 3, values: []string{"a", "b", "c"}}, + "test.float32s": &array[float32]{size: 3, values: []float32{1.0, 2.0, 3.0}}, + "test.int32s": &array[int32]{size: 3, values: []int32{1, 2, 3}}, + "test.uint32s": &array[uint32]{size: 3, values: []uint32{1, 2, 3}}, + } + + if diff := cmp.Diff(kv.Strings("strings"), []string{"a", "b", "c"}); diff != "" { + t.Errorf("unexpected strings (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Strings("nonexistent.strings"), []string(nil)); diff != "" { + t.Errorf("unexpected strings (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Strings("default.strings", []string{"ollama"}), []string{"ollama"}); diff != "" { + t.Errorf("unexpected strings (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Floats("float32s"), []float32{1.0, 2.0, 3.0}); diff != "" { + t.Errorf("unexpected float32s (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Floats("nonexistent.float32s"), []float32(nil)); diff != "" { + t.Errorf("unexpected float32s (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Floats("default.float32s", []float32{math.MaxFloat32}), []float32{math.MaxFloat32}); diff != "" { + t.Errorf("unexpected float32s (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Ints("int32s"), []int32{1, 2, 3}); diff != "" { + t.Errorf("unexpected int8s (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Ints("nonexistent.int32s"), []int32(nil)); diff != "" { + t.Errorf("unexpected int8s (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Ints("default.int32s", []int32{math.MaxInt32}), []int32{math.MaxInt32}); diff != "" { + t.Errorf("unexpected int8s (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Uints("uint32s"), []uint32{1, 2, 3}); diff != "" { + t.Errorf("unexpected uint8s (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Uints("nonexistent.uint32s"), []uint32(nil)); diff != "" { + t.Errorf("unexpected uint8s (-got +want):\n%s", diff) + } + + if diff := cmp.Diff(kv.Uints("default.uint32s", []uint32{math.MaxUint32}), []uint32{math.MaxUint32}); diff != "" { + t.Errorf("unexpected uint8s (-got +want):\n%s", diff) + } +}