/* * value_test.c - classes de valor (despacho por kind) e TinyBlocks_Result. */ #include #include #include #include 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; } static size_t handle_text(TinyBlocks_Block* self, char* out, size_t cap) { (void)self; return (size_t)snprintf(out, cap, "tensor [3, 32, 32]"); } 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 && tb_object_refs(ve.as.result) == 2, "clone de resultado divide a referência"); tb_value_free(&c); CHECK(tb_object_refs(ve.as.result) == 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); /* Bloco-handle: o tipo diz como o valor vira texto (value_text) */ TinyBlocks* rt = NULL; tinyblocks_init(&rt, NULL); TinyBlocks_TypeBlock* th = tb_type_block_create(rt, "Teste._Handle", NULL, 0, NULL, 0, NULL); TinyBlocks_Block* hb = tb_block_create(rt, th); CHECK(text_is(tb_value_block(hb), "[bloco Teste._Handle]"), "bloco sem value_text: \"[bloco Tipo]\""); tb_type_block_set_value_text(th, handle_text); CHECK(text_is(tb_value_block(hb), "tensor [3, 32, 32]"), "bloco com value_text: o texto do tipo"); tinyblocks_deinit(&rt); if (fails) printf("\n%d FALHA(S)\n", fails); else printf("\nTUDO OK\n"); return fails != 0; }