LLM inference in C/C++
C++ ggml
jinja : implement sameas test (#29448)
Sigbjørn Skjæret81bc6b83f827
diff --git a/common/jinja/value.cpp b/common/jinja/value.cppindex 6999ef7d67..ce4c66f879 100644--- a/common/jinja/value.cpp+++ b/common/jinja/value.cpp@@ -515,8 +515,28 @@ const func_builtins & global_builtins() {}},{"test_is_sameas", [](const func_args & args) -> value {// Check if an object points to the same memory address as another object- (void)args;- throw not_implemented_exception("sameas test not implemented");+ args.ensure_count(2);+ auto a = args.get_pos(0);+ auto b = args.get_pos(1);+ bool res = false;+ if (!is_val<value_undefined>(a) && !is_val<value_undefined>(b)) {+ if (is_val<value_none>(a) && is_val<value_none>(b)) {+ res = true;+ } else if (is_val<value_bool>(a) && is_val<value_bool>(b)) {+ if (a->as_bool() == b->as_bool()) {+ res = true;+ }+ } else if (is_val<value_int>(a) && is_val<value_int>(b)) {+ const int64_t x = a->as_int();+ // Allow comparison within small-int cache range+ if (x >= -5 && x <= 256 && x == b->as_int()) {+ res = true;+ }+ } else if (a == b) {+ res = true;+ }+ }+ return mk_val<value_bool>(res);}},{"test_is_escaped", [](const func_args & args) -> value {(void)args;diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cppindex 064f5927ba..50a79af3f3 100644--- a/tests/test-jinja.cpp+++ b/tests/test-jinja.cpp@@ -1212,12 +1212,36 @@ static void test_tests(testing & t) {"yes");- test_template(t, "is sameas",+ test_template(t, "is sameas boolean","{{ 'yes' if x is sameas(false) }}",{{"x", false}},"yes");+ test_template(t, "is sameas integer",+ "{{ 'yes' if x is sameas(1) }}",+ {{"x", 1}},+ "yes"+ );++ test_template(t, "is sameas object",+ "{{ 'yes' if x is sameas(x) }}",+ {{"x", {{"y", false}}}},+ "yes"+ );++ test_template(t, "is sameas ref object",+ "{% set y = x.y %}{{ 'yes' if x.y is sameas(y) and x.y is not sameas(x.z) }}",+ {{"x", {{"y", {{"z", 1}}}, {"z", {{"z", 1}}}}}},+ "yes"+ );++ test_template(t, "is sameas undefined",+ "{{ 'yes' if x is sameas(x) else 'no' }}",+ json::object(),+ "no"+ );+test_template(t, "is boolean","{{ 'yes' if x is boolean }}",{{"x", true}},