Libraries os, string, number e vector (listas); pastas e booleanos no fs

- tinyblocks-fs: itens da pasta (lista), quantidade/item N da pasta e os
  booleanos "é arquivo?", "é pasta?" e "existe?".
- tinyblocks-os (categoria Sistema): sistema operacional, distribuição,
  versão do kernel, arquitetura, computador, usuário, pastas, CPUs,
  memória, tempo ligado e "o sistema é [ ]?".
- tinyblocks-string (Texto): maiúsculas/minúsculas, trecho, letra N,
  posição, troca, apara, repete, inverte, divide em lista e testes;
  conta letras UTF-8.
- tinyblocks-number (Números): reais, potência, raiz, arredondamentos,
  trigonometria em graus, logaritmos, pi, aleatório, limite, formatação,
  par/primo/inteiro.
- tinyblocks-vector (Listas, blocos List.*): listas JSON imutáveis com
  adicionar, inserir, remover, trocar, item N, posição, fatia, ordenar,
  juntar, soma; campos aceitam "[1,2]" ou "a, b". O exemplo de desenho
  vetorial passa a se chamar tinyblocks-vector-draw.
- Editor: saída única cujo nome termina em '?' vira bloco booleano
  (hexágono); listas JSON aparecem como texto no "escreva"; rótulos PT.
- Testes: make test-os e make test-stdlib.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-22 13:10:09 -03:00
1 parent f0abc7593a
commit 33705b2903
25 files changed
+2262 -9

No files matched your search

+145
View File
@@ -0,0 +1,145 @@
/*
* os_fs_test.c - testa tinyblocks-os (informacoes do sistema) e os blocos
* de pasta do tinyblocks-fs (FS.ListDir / ListCount / ListItem / IsFile /
* IsDir / Exists).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_json_block.h>
#include <tb_fs.h>
#include <tb_os.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 TinyBlocks* rt;
/* Cria o bloco, preenche as entradas (texto/inteiro) e avalia */
static TinyBlocks_Block* run(const char* type, const char* a, long long b) {
TinyBlocks_TypeBlock* t = tb_rt_find_type(rt, type);
if (!t) return NULL;
TinyBlocks_Block* blk = tb_block_create(rt, t);
if (a) {
TinyBlocks_Value v = tb_value_text(a);
tb_block_set_default(blk, 0, v);
tb_value_free(&v);
}
if (b >= 0) {
TinyBlocks_Value v = tb_value_integer(b);
tb_block_set_default(blk, 1, v);
tb_value_free(&v);
}
tb_block_eval(blk, rt);
return blk;
}
static long long out_int(TinyBlocks_Block* b) {
return (b && b->outputs[0].kind == TB_VALUE_INTEGER) ? b->outputs[0].as.integer : -1;
}
static const char* out_text(TinyBlocks_Block* b) {
return (b && b->outputs[0].kind == TB_VALUE_TEXT && b->outputs[0].as.text) ? b->outputs[0].as.text : "";
}
static void touch(const char* p) {
FILE* f = fopen(p, "wb");
if (f) { fputs("x", f); fclose(f); }
}
int main(void) {
tinyblocks_init(&rt, NULL);
TinyBlocks_LibraryBlock* fs = tb_library_register(rt, tb_fs_get_library());
TinyBlocks_LibraryBlock* os = tb_library_register(rt, tb_os_get_library());
CHECK(fs && os, "libraries fs e os registradas");
const TinyBlocks_Category* cat = os ? tb_library_category(os) : NULL;
CHECK(cat && !strcmp(cat->name, "Sistema"), "categoria 'Sistema'");
/* -- tinyblocks-os -- */
char buf[512];
tb_os_name(buf, sizeof(buf));
CHECK(buf[0], "tb_os_name devolve um nome");
TinyBlocks_Block* b = run("OS.Name", NULL, -1);
CHECK(!strcmp(out_text(b), buf), "OS.Name igual a tb_os_name");
tb_block_destroy(b, rt);
b = run("OS.Distro", NULL, -1);
CHECK(out_text(b)[0], "OS.Distro devolve texto");
tb_block_destroy(b, rt);
b = run("OS.Version", NULL, -1);
CHECK(out_text(b)[0], "OS.Version devolve a versao do kernel");
tb_block_destroy(b, rt);
b = run("OS.Arch", NULL, -1);
CHECK(out_text(b)[0], "OS.Arch devolve a arquitetura");
tb_block_destroy(b, rt);
b = run("OS.User", NULL, -1);
CHECK(out_text(b)[0], "OS.User devolve o usuario");
tb_block_destroy(b, rt);
b = run("OS.Cpus", NULL, -1);
CHECK(out_int(b) >= 1, "OS.Cpus >= 1");
tb_block_destroy(b, rt);
b = run("OS.MemoryMB", NULL, -1);
CHECK(out_int(b) > 0, "OS.MemoryMB > 0");
tb_block_destroy(b, rt);
b = run("OS.Is", buf, -1);
CHECK(out_int(b) == 1, "OS.Is(nome do SO) verdadeiro");
tb_block_destroy(b, rt);
b = run("OS.Is", "SistemaQueNaoExiste", -1);
CHECK(out_int(b) == 0, "OS.Is(outro) falso");
tb_block_destroy(b, rt);
TinyBlocks_TypeBlock* tis = tb_rt_find_type(rt, "OS.Is");
CHECK(tis && tis->output_desc_count == 1 &&
tis->output_descs[0].name[strlen(tis->output_descs[0].name) - 1] == '?',
"OS.Is declara saida booleana ('?')");
/* -- tinyblocks-fs: pastas -- */
char dir[] = "/tmp/tb_osfs_XXXXXX";
CHECK(mkdtemp(dir) != NULL, "pasta temporaria");
char p[256];
snprintf(p, sizeof(p), "%s/b.txt", dir); touch(p);
snprintf(p, sizeof(p), "%s/A.json", dir); touch(p);
snprintf(p, sizeof(p), "%s/sub", dir); mkdir(p, 0755);
b = run("FS.ListCount", dir, -1);
CHECK(out_int(b) == 3, "FS.ListCount = 3");
tb_block_destroy(b, rt);
b = run("FS.ListDir", dir, -1);
bool list_ok = b && b->outputs[0].kind == TB_VALUE_JSON;
if (list_ok) {
TinyBlocks_JsonValue* arr = b->outputs[0].as.json;
list_ok = tb_json_array_length(arr) == 3;
TinyBlocks_JsonValue* first = list_ok ? tb_json_array_get(arr, 0) : NULL;
list_ok = list_ok && first && !strcmp(tb_json_as_string(first), "A.json");
if (first) tb_json_release(first);
}
CHECK(list_ok, "FS.ListDir devolve lista ordenada [A.json, b.txt, sub]");
tb_block_destroy(b, rt);
b = run("FS.ListItem", dir, 2);
CHECK(!strcmp(out_text(b), "b.txt"), "FS.ListItem(2) = b.txt");
tb_block_destroy(b, rt);
b = run("FS.ListItem", dir, 9);
CHECK(!strcmp(out_text(b), ""), "FS.ListItem fora da faixa = vazio");
tb_block_destroy(b, rt);
snprintf(p, sizeof(p), "%s/b.txt", dir);
b = run("FS.IsFile", p, -1); CHECK(out_int(b) == 1, "FS.IsFile(arquivo) verdadeiro"); tb_block_destroy(b, rt);
b = run("FS.IsDir", p, -1); CHECK(out_int(b) == 0, "FS.IsDir(arquivo) falso"); tb_block_destroy(b, rt);
b = run("FS.Exists", p, -1); CHECK(out_int(b) == 1, "FS.Exists(arquivo) verdadeiro"); tb_block_destroy(b, rt);
remove(p);
snprintf(p, sizeof(p), "%s/sub", dir);
b = run("FS.IsDir", p, -1); CHECK(out_int(b) == 1, "FS.IsDir(pasta) verdadeiro"); tb_block_destroy(b, rt);
b = run("FS.IsFile", p, -1); CHECK(out_int(b) == 0, "FS.IsFile(pasta) falso"); tb_block_destroy(b, rt);
rmdir(p);
snprintf(p, sizeof(p), "%s/A.json", dir);
remove(p);
b = run("FS.Exists", p, -1); CHECK(out_int(b) == 0, "FS.Exists(apagado) falso"); tb_block_destroy(b, rt);
rmdir(dir);
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}