Files
tinyblocks/tests/qr_test.c
T
mguinhosandClaude Opus 5.5 4f6a68c22c Editor: animacoes M3, compartilhar/receber blocos por QR e ver codigo-fonte
- Animacoes (curvas do M3): blocos criados por codigo surgem em cascata,
  apagados saem de cena (fantasma que encolhe), os de baixo deslizam ao
  mudar de lugar na pilha; rolagem e zoom suaves. TINYBLOCKS_REDUCE_MOTION=1
  desliga (o harness usa). Texto respeita a opacidade no backend SDL2.
- Botao direito: "Ver codigo-fonte" (funcao C do bloco na aba de codigo),
  "Compartilhar pilha/blueprint (QR)" (scratchblocks em partes de 220 bytes,
  "TB1|id|parte|total|", em loop de 1 s) e "Receber blocos (QR)" (camera,
  partes em qualquer ordem, importa onde clicou).
- tinyblocks-qr: qrcodegen (MIT) + quirc (ISC) e camera em cinza: V4L2 no
  Linux, Camera2 do NDK no Android (sem Java), nada no Windows.
- Captura de desenvolvimento: TINYBLOCKS_SHOT_COUNT/STEP salvam sequencias.
- Testes: test-qr (ida e volta) e harness cenario 35.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 23:28:18 -03:00

48 lines
1.9 KiB
C

/* Teste da tinyblocks-qr: gerar um QR, desenhar numa imagem cinza e ler */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tb_qr.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 char got[4096];
static void found(const char* t, int n, void* u) { (void)u; snprintf(got, sizeof(got), "%.*s", n, t); }
/* QR -> imagem cinza: s pixels por modulo, margem de 4 modulos, fundo claro */
static int roundtrip(const char* text, int s) {
static unsigned char mods[TB_QR_MAX_SIDE * TB_QR_MAX_SIDE];
int side = tb_qr_encode(text, mods, (int)sizeof(mods));
if (!side) return 0;
int W = (side + 8) * s, H = W;
unsigned char* img = malloc((size_t)W * H);
memset(img, 230, (size_t)W * H);
for (int y = 0; y < side; y++)
for (int x = 0; x < side; x++)
if (mods[y * side + x])
for (int dy = 0; dy < s; dy++)
memset(img + (size_t)((y + 4) * s + dy) * W + (x + 4) * s, 25, (size_t)s);
TbQrReader* r = tb_qr_reader_new();
got[0] = 0;
int n = tb_qr_reader_scan(r, img, W, H, found, NULL);
tb_qr_reader_free(r);
free(img);
return n;
}
int main(void) {
const char* small = "TB1|a1b2|1|1|when flag clicked\nsay [olá]\n";
CHECK(roundtrip(small, 4) == 1 && !strcmp(got, small), "QR pequeno (com acento) vai e volta");
char big[300];
for (int i = 0; i < 299; i++) big[i] = (char)('a' + i % 26);
big[299] = 0;
CHECK(roundtrip(big, 3) == 1 && !strcmp(got, big), "QR de 299 bytes vai e volta");
static unsigned char mods[TB_QR_MAX_SIDE * TB_QR_MAX_SIDE];
char huge[4000];
memset(huge, 'x', sizeof(huge) - 1); huge[sizeof(huge) - 1] = 0;
CHECK(tb_qr_encode(huge, mods, (int)sizeof(mods)) == 0, "texto grande demais: nao gera");
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
return fails != 0;
}