Result como tagged union e classes de valor (polimorfismo dos valores)
- TinyBlocks_Result (tb_result.h): Ok | Erro com tag (enum) e payload,
ref-counted, que flui como TB_VALUE_RESULT. A library de erros passa a
produzir/consumir Result em vez de objetos JSON {"ok"/"erro"}.
- TinyBlocks_ValueClass (tb_value_class.h): cada kind de valor tem uma
"interface" (copy, release, to_text, to_number, truthy, equals,
type_name) numa tabela; tb_value_clone/free e as operacoes genericas
(tb_value_to_text, tb_value_to_number, tb_value_truthy, tb_value_equals,
tb_value_type_name) despacham por ela. Sem libm no core.
- Editor e as conversoes "texto de" / "número de" usam o despacho.
- make test-value: classes de valor e Result.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9f6a1a0eb0
commit
351e85bb14
13 files changed
+650
-290
No files matched your search
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* value_test.c - classes de valor (despacho por kind) e TinyBlocks_Result.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
static int fails = 0;
|
||||
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
|
||||
|
||||
static bool text_is(TinyBlocks_Value v, const char* s) {
|
||||
char buf[256];
|
||||
tb_value_to_text(v, buf, sizeof(buf));
|
||||
if (strcmp(buf, s)) printf(" obtido: %s\n", buf);
|
||||
return strcmp(buf, s) == 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
/* toda kind tem classe; desconhecida cai em "nada" */
|
||||
bool all = true;
|
||||
for (int k = 0; k < TB_VALUE_KIND_COUNT; k++)
|
||||
if (tb_value_class((TinyBlocks_ValueKind)k)->kind != (TinyBlocks_ValueKind)k) all = false;
|
||||
CHECK(all, "cada kind tem a sua classe na tabela");
|
||||
CHECK(tb_value_class((TinyBlocks_ValueKind)99)->kind == TB_VALUE_NONE, "kind desconhecido usa a classe de \"nada\"");
|
||||
|
||||
/* texto / numero / verdade */
|
||||
TinyBlocks_Value t = tb_value_text(" 3,5 ");
|
||||
double d = 0;
|
||||
CHECK(tb_value_to_number(t, &d) && d == 3.5, "texto \" 3,5 \" vira 3.5 (vírgula decimal)");
|
||||
CHECK(text_is(tb_value_integer(42), "42") && text_is(tb_value_real(0.1), "0.1"), "números viram texto sem lixo");
|
||||
CHECK(tb_value_as_integer(tb_value_real(2.5), 0) == 3 && tb_value_as_integer(tb_value_real(-2.5), 0) == -3,
|
||||
"as_integer arredonda (2.5 -> 3, -2.5 -> -3)");
|
||||
TinyBlocks_Value f = tb_value_text("Falso");
|
||||
CHECK(!tb_value_truthy(f) && tb_value_truthy(t) && !tb_value_truthy(tb_value_integer(0)), "verdadeiro/falso por classe");
|
||||
TinyBlocks_Value dois = tb_value_text("2");
|
||||
CHECK(tb_value_equals(dois, tb_value_real(2.0)), "\"2\" == 2.0 (números pelo valor)");
|
||||
tb_value_free(&dois);
|
||||
tb_value_free(&f);
|
||||
|
||||
/* JSON despacha pela classe JSON */
|
||||
TinyBlocks_JsonValue* arr = tb_json_parse("[1,\"a\"]", NULL, 0);
|
||||
TinyBlocks_Value jl = tb_value_json(arr);
|
||||
tb_json_release(arr);
|
||||
CHECK(text_is(jl, "[1,\"a\"]") && !strcmp(tb_value_type_name(jl), "lista"), "lista: texto JSON e nome \"lista\"");
|
||||
|
||||
/* Result: tagged union Ok | Erro */
|
||||
TinyBlocks_Result* ok = tb_result_ok(t);
|
||||
TinyBlocks_Value vo = tb_value_result(ok);
|
||||
tb_result_release(ok);
|
||||
CHECK(vo.kind == TB_VALUE_RESULT && tb_value_as_result(vo)->tag == TB_RESULT_OK, "Ok é TB_VALUE_RESULT com tag OK");
|
||||
CHECK(tb_value_is_ok(vo) && !tb_value_is_err(vo) && tb_value_truthy(vo), "Ok é ok e verdadeiro");
|
||||
TinyBlocks_Result* er = tb_result_err_text("não achei");
|
||||
TinyBlocks_Value ve = tb_value_result(er);
|
||||
tb_result_release(er);
|
||||
CHECK(tb_value_is_err(ve) && !tb_value_truthy(ve), "Erro é erro e falso");
|
||||
CHECK(text_is(ve, "Erro(não achei)") && !strcmp(tb_value_type_name(ve), "resultado (erro)"), "texto e nome do Erro");
|
||||
TinyBlocks_Value c = tb_value_clone(ve);
|
||||
CHECK(c.as.result == ve.as.result && ve.as.result->ref == 2, "clone de resultado divide a referência");
|
||||
tb_value_free(&c);
|
||||
CHECK(ve.as.result->ref == 1, "free solta a referência");
|
||||
TinyBlocks_Result* er2 = tb_result_err_text("não achei");
|
||||
TinyBlocks_Value ve2 = tb_value_result(er2);
|
||||
tb_result_release(er2);
|
||||
CHECK(tb_value_equals(ve, ve2) && !tb_value_equals(ve, vo), "resultados iguais pela tag e pelo conteúdo");
|
||||
|
||||
tb_value_free(&vo); tb_value_free(&ve); tb_value_free(&ve2); tb_value_free(&jl); tb_value_free(&t);
|
||||
if (fails) printf("\n%d FALHA(S)\n", fails);
|
||||
else printf("\nTUDO OK\n");
|
||||
return fails != 0;
|
||||
}
|
||||
Reference in new issue
Block a user