Adiciona tinyblocks-base64 e tinyblocks-curves (listas de pontos)

This commit is contained in:
mguinhos committed 2026-10-20 20:59:17 -03:00
1 parent a14c7f9937
commit b1b15a4c49
12 files changed
+815 -9

No files matched your search

+91
View File
@@ -0,0 +1,91 @@
/*
* base64_test.c - testa tinyblocks-base64: encode/decode (RFC 4648) e os
* blocos Base64.Encode / Base64.Decode.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_base64.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 void set_in(TinyBlocks_Block* b, uint32_t i, const char* v) {
TinyBlocks_Value vv = tb_value_text(v);
tb_block_set_default(b, i, vv);
tb_value_free(&vv);
}
int main(void) {
/* -- API C --- */
char* e = tb_base64_encode_str("Ola");
CHECK(e && strcmp(e, "T2xh") == 0, "encode 'Ola' -> 'T2xh'");
free(e);
e = tb_base64_encode_str("");
CHECK(e && strcmp(e, "") == 0, "encode vazio -> vazio");
free(e);
e = tb_base64_encode("f", 1); /* 1 byte -> 2 chars + '==' */
CHECK(e && strcmp(e, "Zg==") == 0, "encode 1 byte -> 'Zg=='");
free(e);
e = tb_base64_encode("fo", 2);
CHECK(e && strcmp(e, "Zm8=") == 0, "encode 2 bytes -> 'Zm8='");
free(e);
char* d = tb_base64_decode_str("T2xh", NULL);
CHECK(d && strcmp(d, "Ola") == 0, "decode 'T2xh' -> 'Ola'");
free(d);
size_t n = 0;
unsigned char* raw = tb_base64_decode("Zm9vYmFy", &n); /* "foobar" */
CHECK(raw && n == 6 && memcmp(raw, "foobar", 6) == 0, "decode 'Zm9vYmFy' -> 'foobar'");
free(raw);
/* round-trip de bytes arbitrarios (0..255) */
unsigned char orig[256];
for (int i = 0; i < 256; i++) orig[i] = (unsigned char)i;
e = tb_base64_encode(orig, sizeof(orig));
raw = tb_base64_decode(e, &n);
CHECK(raw && n == 256 && memcmp(raw, orig, 256) == 0, "round-trip 256 bytes");
free(raw); free(e);
/* entrada invalida */
CHECK(tb_base64_decode("@@@", NULL) == NULL, "decode invalido -> NULL");
CHECK(strlen(tb_base64_last_error()) > 0, "mensagem de erro presente");
/* -- Blocos --- */
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
tb_library_register(rt, tb_base64_get_library());
TinyBlocks_TypeBlock* tenc = tb_rt_find_type(rt, "Base64.Encode");
TinyBlocks_TypeBlock* tdec = tb_rt_find_type(rt, "Base64.Decode");
CHECK(tenc && tdec, "blocos Base64.Encode/Decode registrados");
const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)tenc->base.parent);
CHECK(cat && strcmp(cat->name, "Base64") == 0, "categoria 'Base64'");
if (tenc) {
TinyBlocks_Block* b = tb_block_create(rt, tenc);
set_in(b, 0, "TinyBlocks");
tb_block_eval(b, rt);
CHECK(b->outputs[0].kind == TB_VALUE_TEXT && b->outputs[0].as.text &&
strcmp(b->outputs[0].as.text, "VGlueUJsb2Nrcw==") == 0, "bloco Encode('TinyBlocks')");
tb_block_destroy(b, rt);
}
if (tdec) {
TinyBlocks_Block* b = tb_block_create(rt, tdec);
set_in(b, 0, "VGlueUJsb2Nrcw==");
tb_block_eval(b, rt);
CHECK(b->outputs[0].kind == TB_VALUE_TEXT && b->outputs[0].as.text &&
strcmp(b->outputs[0].as.text, "TinyBlocks") == 0, "bloco Decode(...) -> 'TinyBlocks'");
tb_block_destroy(b, rt);
}
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}
+124
View File
@@ -0,0 +1,124 @@
/*
* curves_test.c - testa tinyblocks-curves: retorno de listas de pontos
* ([[x,y],...]) e os blocos Curves.Line / Bezier / Quadratic / Arc / Circle /
* Count / PointX / PointY.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_json_block.h>
#include <tb_curves.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 void set_num(TinyBlocks_Block* b, uint32_t i, double v) {
TinyBlocks_Value vv = tb_value_real(v);
tb_block_set_default(b, i, vv);
tb_value_free(&vv);
}
static void set_pts(TinyBlocks_Block* b, uint32_t i, TinyBlocks_JsonValue* pts) {
TinyBlocks_Value vv = tb_value_json(pts);
tb_block_set_default(b, i, vv);
tb_value_free(&vv);
}
static bool near(double a, double b) { return fabs(a - b) < 1e-6; }
int main(void) {
/* -- API C: reta ---- */
TinyBlocks_JsonValue* ln = tb_curves_line(0, 0, 10, 20, 4);
CHECK(tb_curves_count(ln) == 5, "reta com 4 passos -> 5 pontos");
double x, y;
CHECK(tb_curves_point(ln, 2, &x, &y) && near(x, 5) && near(y, 10), "reta: ponto do meio (5,10)");
CHECK(tb_curves_point(ln, 4, &x, &y) && near(x, 10) && near(y, 20), "reta: ponto final (10,20)");
CHECK(!tb_curves_point(ln, 5, &x, &y), "reta: indice fora -> false");
/* -- Bezier cubica: extremos e simetria --- */
TinyBlocks_JsonValue* bz = tb_curves_bezier(0, 0, 0, 10, 10, 10, 10, 0, 8);
CHECK(tb_curves_count(bz) == 9, "bezier com 8 passos -> 9 pontos");
tb_curves_point(bz, 0, &x, &y);
CHECK(near(x, 0) && near(y, 0), "bezier: comeca em (0,0)");
tb_curves_point(bz, 8, &x, &y);
CHECK(near(x, 10) && near(y, 0), "bezier: termina em (10,0)");
/* t=0.5 -> (5, 7.5) para essa curva */
tb_curves_point(bz, 4, &x, &y);
CHECK(near(x, 5) && near(y, 7.5), "bezier: ponto medio (5,7.5)");
/* -- Quadratica --- */
TinyBlocks_JsonValue* qd = tb_curves_quadratic(0, 0, 5, 10, 10, 0, 2);
tb_curves_point(qd, 1, &x, &y);
CHECK(near(x, 5) && near(y, 5), "quadratica: topo (5,5)");
/* -- Arco / circulo --- */
TinyBlocks_JsonValue* ar = tb_curves_arc(0, 0, 10, 0, 90, 2);
tb_curves_point(ar, 0, &x, &y);
CHECK(near(x, 10) && near(y, 0), "arco: comeca em (10,0)");
tb_curves_point(ar, 2, &x, &y);
CHECK(near(x, 0) && near(y, 10), "arco: termina em (0,10)");
TinyBlocks_JsonValue* ci = tb_curves_circle(0, 0, 5, 4);
CHECK(tb_curves_count(ci) == 5, "circulo fechado com 4 passos -> 5 pontos");
tb_curves_point(ci, 0, &x, &y);
tb_curves_point(ci, 4, &x, &y);
CHECK(near(x, 5) && near(y, 0), "circulo: fecha no ponto inicial");
/* limites de passos */
TinyBlocks_JsonValue* z = tb_curves_line(0, 0, 1, 1, 0);
CHECK(tb_curves_count(z) == 2, "passos=0 vira 1 -> 2 pontos");
tb_json_release(z);
tb_json_release(ln); tb_json_release(bz); tb_json_release(qd);
tb_json_release(ar); tb_json_release(ci);
/* -- Blocos --- */
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
tb_library_register(rt, tb_curves_get_library());
const char* names[] = {"Curves.Line","Curves.Bezier","Curves.Quadratic",
"Curves.Arc","Curves.Circle","Curves.Count",
"Curves.PointX","Curves.PointY"};
int all = 1;
for (size_t i = 0; i < sizeof(names)/sizeof(names[0]); i++)
if (!tb_rt_find_type(rt, names[i])) { all = 0; printf(" falta %s\n", names[i]); }
CHECK(all, "todos os blocos Curves.* registrados");
TinyBlocks_TypeBlock* tl = tb_rt_find_type(rt, "Curves.Line");
const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)tl->base.parent);
CHECK(cat && strcmp(cat->name, "Curvas") == 0, "categoria 'Curvas'");
/* Curves.Line via bloco */
TinyBlocks_Block* bl = tb_block_create(rt, tl);
set_num(bl, 0, 0); set_num(bl, 1, 0);
set_num(bl, 2, 8); set_num(bl, 3, 4);
set_num(bl, 4, 2);
tb_block_eval(bl, rt);
CHECK(bl->outputs[0].kind == TB_VALUE_JSON, "Curves.Line devolve JSON");
CHECK(tb_curves_count(bl->outputs[0].as.json) == 3, "Curves.Line devolve 3 pontos");
/* Curves.Count recebe a lista do bloco anterior */
TinyBlocks_TypeBlock* tc = tb_rt_find_type(rt, "Curves.Count");
TinyBlocks_Block* bc = tb_block_create(rt, tc);
set_pts(bc, 0, bl->outputs[0].as.json);
tb_block_eval(bc, rt);
CHECK(bc->outputs[0].as.integer == 3, "Curves.Count -> 3");
/* Curves.PointX(2) da lista */
TinyBlocks_TypeBlock* tpx = tb_rt_find_type(rt, "Curves.PointX");
TinyBlocks_Block* bpx = tb_block_create(rt, tpx);
set_pts(bpx, 0, bl->outputs[0].as.json);
TinyBlocks_Value idx = tb_value_integer(2);
tb_block_set_default(bpx, 1, idx);
tb_block_eval(bpx, rt);
CHECK(near(bpx->outputs[0].as.real, 8.0), "Curves.PointX(2) -> 8");
tb_block_destroy(bl, rt); tb_block_destroy(bc, rt); tb_block_destroy(bpx, rt);
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}