ref: 2b804dcfaf643d9fbfb4dbb0450b3b3e37c23607
dir: /rle_test.c/
#include "rle.c"
int rletests(void);
static int
testmemfill1(void)
{
/* Fill with a pattern shorter than the destination (pattern repeats) */
uchar dst[6];
uchar pat[] = {0xAB, 0xCD};
uchar want[] = {0xAB, 0xCD, 0xAB, 0xCD, 0xAB, 0xCD};
int i;
memfill(dst, sizeof dst, pat, sizeof pat);
for(i = 0; i < (int)sizeof dst; i++)
if(dst[i] != want[i])
sysfatal("testmemfill1: dst[%d]: want 0x%x, got 0x%x", i, want[i], dst[i]);
return 0;
}
static int
testmemfill2(void)
{
/* Fill with a pattern equal in length to the destination */
uchar dst[4];
uchar pat[] = {0x01, 0x02, 0x03, 0x04};
int i;
memfill(dst, sizeof dst, pat, sizeof pat);
for(i = 0; i < (int)sizeof dst; i++)
if(dst[i] != pat[i])
sysfatal("testmemfill2: dst[%d]: want 0x%x, got 0x%x", i, pat[i], dst[i]);
return 0;
}
static int
testmemfill3(void)
{
/* Fill a single byte with a single-byte pattern */
uchar dst[1] = {0x00};
uchar pat[] = {0x42};
memfill(dst, sizeof dst, pat, sizeof pat);
if(dst[0] != 0x42)
sysfatal("testmemfill3: dst[0]: want 0x42, got 0x%x", dst[0]);
return 0;
}
static int
testmemfill4(void)
{
/* Zero-length destination: no bytes written, returns a1 */
uchar dst[1] = {0x99};
uchar pat[] = {0x42};
void *ret;
ret = memfill(dst, 0, pat, sizeof pat);
if(ret != dst)
sysfatal("testmemfill4: return value: want dst, got something else");
if(dst[0] != 0x99)
sysfatal("testmemfill4: dst[0]: want 0x99, got 0x%x", dst[0]);
return 0;
}
static int
testmemxor1(void)
{
/* Basic XOR of two equal-length arrays */
uchar a[] = {0xFF, 0x0F, 0xAA};
uchar b[] = {0x0F, 0xFF, 0x55};
uchar want[] = {0xF0, 0xF0, 0xFF};
int i;
memxor(a, b, sizeof a);
for(i = 0; i < (int)sizeof a; i++)
if((uchar)a[i] != want[i])
sysfatal("testmemxor1: a[%d]: want 0x%x, got 0x%x", i, want[i], (uchar)a[i]);
return 0;
}
static int
testmemxor2(void)
{
/* XOR with zero-length: no bytes modified */
uchar a[] = {0xAB, 0xCD};
uchar b[] = {0xFF, 0xFF};
memxor(a, b, 0);
if(a[0] != 0xAB || a[1] != 0xCD)
sysfatal("testmemxor2: unexpected modification: got 0x%x 0x%x", a[0], a[1]);
return 0;
}
static int
testmemxor3(void)
{
/* XOR with self yields all zeros */
uchar a[] = {0x12, 0x34, 0x56};
int i;
memxor(a, a, sizeof a);
for(i = 0; i < (int)sizeof a; i++)
if(a[i] != 0)
sysfatal("testmemxor3: a[%d]: want 0, got 0x%x", i, a[i]);
return 0;
}
static int
testmemxor4(void)
{
/* XOR with all-zero array leaves original unchanged */
uchar a[] = {0xDE, 0xAD, 0xBE, 0xEF};
uchar z[] = {0x00, 0x00, 0x00, 0x00};
uchar orig[] = {0xDE, 0xAD, 0xBE, 0xEF};
int i;
memxor(a, z, sizeof a);
for(i = 0; i < (int)sizeof a; i++)
if(a[i] != orig[i])
sysfatal("testmemxor4: a[%d]: want 0x%x, got 0x%x", i, orig[i], a[i]);
return 0;
}
static int
testunrle1(void)
{
/* Bpix extended opcode (0xFE): sets the current pixel to zero */
uchar src[] = {0xFE};
uchar dst[1];
uchar *end;
end = unrle(dst, sizeof dst, src, sizeof src, 2, 1);
if(end == nil)
sysfatal("testunrle1: unexpected error: %r");
if(end - dst != 1)
sysfatal("testunrle1: length: want 1, got %d", (int)(end - dst));
if(dst[0] != 0)
sysfatal("testunrle1: pixel: want 0, got %d", dst[0]);
return 0;
}
static int
testunrle2(void)
{
/*
* Bg opcode on the first scan line (no previous line):
* memset(wp, 0, len) zero-fills the output.
* hdr=0x01: standard header, code=0 (Bg), raw_len=1 → len=1 pixel.
*/
uchar src[] = {0x01};
uchar dst[1];
uchar *end;
end = unrle(dst, sizeof dst, src, sizeof src, 4, 1);
if(end == nil)
sysfatal("testunrle2: unexpected error: %r");
if(end - dst != 1)
sysfatal("testunrle2: length: want 1, got %d", (int)(end - dst));
if(dst[0] != 0)
sysfatal("testunrle2: pixel: want 0, got %d", dst[0]);
return 0;
}
static int
testunrle3(void)
{
/*
* Mix opcode on the first scan line with an all-zero mask byte:
* memset(wp, 0, pixelsize) zero-fills each pixel whose mask bit is 0.
* hdr=0x41: standard header, code=4 (Mix), raw_len=1 → len=8 pixels.
* mask=0x00: all bits 0, so all 8 pixels are zero-filled.
*/
uchar src[] = {0x41, 0x00};
uchar dst[8];
uchar *end;
int i;
memset(dst, 0xFF, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle3: unexpected error: %r");
if(end - dst != 8)
sysfatal("testunrle3: length: want 8, got %d", (int)(end - dst));
for(i = 0; i < 8; i++)
if(dst[i] != 0)
sysfatal("testunrle3: dst[%d]: want 0, got %d", i, dst[i]);
return 0;
}
static int
testunrle4(void)
{
/*
* Lit opcode: code=8, bits=Bits5=31, raw_len=3 → len=3 pixels.
* hdr=0x83: copies 3 literal bytes verbatim into output.
*/
uchar src[] = {0x83, 0xAA, 0xBB, 0xCC};
uchar dst[3];
uchar *end;
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle4: unexpected error: %r");
if(end - dst != 3)
sysfatal("testunrle4: length: want 3, got %d", (int)(end - dst));
if(dst[0] != 0xAA || dst[1] != 0xBB || dst[2] != 0xCC)
sysfatal("testunrle4: pixels: want AA BB CC, got %02x %02x %02x",
dst[0], dst[1], dst[2]);
return 0;
}
static int
testunrle5(void)
{
/*
* Fg opcode on the first scan line (no previous line):
* fills with the initial pen (DWhite=0xFF for pixelsize=1), no XOR.
* hdr=0x24: code=2 (Fg), raw_len=4 → len=4 pixels.
*/
uchar src[] = {0x24};
uchar dst[4];
uchar *end;
int i;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle5: unexpected error: %r");
if(end - dst != 4)
sysfatal("testunrle5: length: want 4, got %d", (int)(end - dst));
for(i = 0; i < 4; i++)
if(dst[i] != 0xFF)
sysfatal("testunrle5: dst[%d]: want 0xFF, got 0x%02x", i, dst[i]);
return 0;
}
static int
testunrle6(void)
{
/*
* FgS opcode: code=12, bits=Bits4=15, raw_len=1 → len=1 pixel.
* hdr=0xC1: reads 1-byte pen (0x42), then fills 1 pixel with it.
* First scan line: no XOR with previous row.
*/
uchar src[] = {0xC1, 0x42};
uchar dst[1];
uchar *end;
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle6: unexpected error: %r");
if(end - dst != 1)
sysfatal("testunrle6: length: want 1, got %d", (int)(end - dst));
if(dst[0] != 0x42)
sysfatal("testunrle6: pixel: want 0x42, got 0x%02x", dst[0]);
return 0;
}
static int
testunrle7(void)
{
/*
* Fill opcode: code=6, bits=Bits5=31, raw_len=3 → len=3 pixels.
* hdr=0x63: reads 1-byte fill colour (0x55), fills 3 pixels with it.
*/
uchar src[] = {0x63, 0x55};
uchar dst[3];
uchar *end;
int i;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle7: unexpected error: %r");
if(end - dst != 3)
sysfatal("testunrle7: length: want 3, got %d", (int)(end - dst));
for(i = 0; i < 3; i++)
if(dst[i] != 0x55)
sysfatal("testunrle7: dst[%d]: want 0x55, got 0x%02x", i, dst[i]);
return 0;
}
static int
testunrle8(void)
{
/*
* Dith opcode: code=14, bits=Bits4=15, raw_len=1 → len=1 (×pixelsize).
* hdr=0xE1: inside Dith, len doubles to 2; reads two 1-byte colours
* {0xAA, 0xBB} and fills output with the alternating pattern.
*/
uchar src[] = {0xE1, 0xAA, 0xBB};
uchar dst[2];
uchar *end;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle8: unexpected error: %r");
if(end - dst != 2)
sysfatal("testunrle8: length: want 2, got %d", (int)(end - dst));
if(dst[0] != 0xAA || dst[1] != 0xBB)
sysfatal("testunrle8: pixels: want AA BB, got %02x %02x", dst[0], dst[1]);
return 0;
}
static int
testunrle9(void)
{
/*
* Wpix extended opcode (0xFD): sets the current pixel to DWhite (0xFF).
*/
uchar src[] = {0xFD};
uchar dst[1];
uchar *end;
dst[0] = 0;
end = unrle(dst, sizeof dst, src, sizeof src, 2, 1);
if(end == nil)
sysfatal("testunrle9: unexpected error: %r");
if(end - dst != 1)
sysfatal("testunrle9: length: want 1, got %d", (int)(end - dst));
if(dst[0] != 0xFF)
sysfatal("testunrle9: pixel: want 0xFF, got 0x%02x", dst[0]);
return 0;
}
static int
testunrle10(void)
{
/*
* Mix opcode with non-trivial mask (0xAA = 10101010):
* hdr=0x41: code=4 (Mix), raw_len=1 → len=8 pixels.
* Even-indexed pixels (bit=0) are zero-filled; odd-indexed (bit=1)
* get the initial pen (DWhite=0xFF). No previous scan line, no XOR.
*/
uchar src[] = {0x41, 0xAA};
uchar dst[8];
uchar *end;
uchar want[] = {0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF};
int i;
memset(dst, 0x55, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle10: unexpected error: %r");
if(end - dst != 8)
sysfatal("testunrle10: length: want 8, got %d", (int)(end - dst));
for(i = 0; i < 8; i++)
if(dst[i] != want[i])
sysfatal("testunrle10: dst[%d]: want 0x%02x, got 0x%02x",
i, want[i], dst[i]);
return 0;
}
static int
testunrle11(void)
{
/*
* MixS opcode: code=13, bits=Bits4=15, raw_len=1 → len=8 pixels.
* hdr=0xD1: reads 1-byte pen (0x42), mask=0xFF (all bits set).
* First scan line, all bits 1: every pixel written with new pen, no XOR.
*/
uchar src[] = {0xD1, 0x42, 0xFF};
uchar dst[8];
uchar *end;
int i;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle11: unexpected error: %r");
if(end - dst != 8)
sysfatal("testunrle11: length: want 8, got %d", (int)(end - dst));
for(i = 0; i < 8; i++)
if(dst[i] != 0x42)
sysfatal("testunrle11: dst[%d]: want 0x42, got 0x%02x", i, dst[i]);
return 0;
}
static int
testunrle12(void)
{
/*
* Mix3 extended opcode (0xF9): fixed bitmask sreg=3 (00000011).
* Produces 8 pixels: first two get pen (DWhite=0xFF), rest are zero.
* First scan line: no XOR with previous row.
*/
uchar src[] = {0xF9};
uchar dst[8];
uchar *end;
uchar want[] = {0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
int i;
memset(dst, 0x55, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle12: unexpected error: %r");
if(end - dst != 8)
sysfatal("testunrle12: length: want 8, got %d", (int)(end - dst));
for(i = 0; i < 8; i++)
if(dst[i] != want[i])
sysfatal("testunrle12: dst[%d]: want 0x%02x, got 0x%02x",
i, want[i], dst[i]);
return 0;
}
static int
testunrle13(void)
{
/*
* Mix5 extended opcode (0xFA): fixed bitmask sreg=5 (00000101).
* Produces 8 pixels: pixels 0 and 2 get pen (DWhite=0xFF), rest zero.
* First scan line: no XOR with previous row.
*/
uchar src[] = {0xFA};
uchar dst[8];
uchar *end;
uchar want[] = {0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00};
int i;
memset(dst, 0x55, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle13: unexpected error: %r");
if(end - dst != 8)
sysfatal("testunrle13: length: want 8, got %d", (int)(end - dst));
for(i = 0; i < 8; i++)
if(dst[i] != want[i])
sysfatal("testunrle13: dst[%d]: want 0x%02x, got 0x%02x",
i, want[i], dst[i]);
return 0;
}
static int
testunrle14(void)
{
/*
* Bg on second scan line: copies pixels from the previous scan line.
* First opcode: Lit 4 pixels {0x11,0x22,0x33,0x44}.
* Second opcode: hdr=0x04 (Bg, raw_len=4) — on second scan line
* wp-bpl points into the already-written first scan line, so
* memmove copies it verbatim (no XOR for Bg).
*/
uchar src[] = {0x84, 0x11, 0x22, 0x33, 0x44, 0x04};
uchar dst[8];
uchar *end;
int i;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 4, 1);
if(end == nil)
sysfatal("testunrle14: unexpected error: %r");
if(end - dst != 8)
sysfatal("testunrle14: length: want 8, got %d", (int)(end - dst));
for(i = 0; i < 4; i++)
if(dst[4+i] != dst[i])
sysfatal("testunrle14: dst[%d]: want 0x%02x, got 0x%02x",
4+i, dst[i], dst[4+i]);
return 0;
}
static int
testunrle15(void)
{
/*
* Fg on second scan line: fills with pen (DWhite) then XORs with
* the previous scan line.
* First opcode: Lit 4 pixels {0x11,0x22,0x33,0x44}.
* Second opcode: hdr=0x24 (Fg, raw_len=4) — pen=0xFF XOR prev.
* Expected: {0xFF^0x11, 0xFF^0x22, 0xFF^0x33, 0xFF^0x44}
* = {0xEE, 0xDD, 0xCC, 0xBB}.
*/
uchar src[] = {0x84, 0x11, 0x22, 0x33, 0x44, 0x24};
uchar dst[8];
uchar *end;
uchar want[] = {0xEE, 0xDD, 0xCC, 0xBB};
int i;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 4, 1);
if(end == nil)
sysfatal("testunrle15: unexpected error: %r");
if(end - dst != 8)
sysfatal("testunrle15: length: want 8, got %d", (int)(end - dst));
for(i = 0; i < 4; i++)
if(dst[4+i] != want[i])
sysfatal("testunrle15: dst[%d]: want 0x%02x, got 0x%02x",
4+i, want[i], dst[4+i]);
return 0;
}
static int
testunrle16(void)
{
/*
* FgS on second scan line: sets a new pen (0xF0) then XORs with
* the previous scan line.
* First opcode: Lit 4 pixels {0x11,0x22,0x33,0x44}.
* Second opcode: hdr=0xC4 (FgS, raw_len=4), pen byte=0xF0.
* Expected: {0xF0^0x11, 0xF0^0x22, 0xF0^0x33, 0xF0^0x44}
* = {0xE1, 0xD2, 0xC3, 0xB4}.
*/
uchar src[] = {0x84, 0x11, 0x22, 0x33, 0x44, 0xC4, 0xF0};
uchar dst[8];
uchar *end;
uchar want[] = {0xE1, 0xD2, 0xC3, 0xB4};
int i;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 4, 1);
if(end == nil)
sysfatal("testunrle16: unexpected error: %r");
if(end - dst != 8)
sysfatal("testunrle16: length: want 8, got %d", (int)(end - dst));
for(i = 0; i < 4; i++)
if(dst[4+i] != want[i])
sysfatal("testunrle16: dst[%d]: want 0x%02x, got 0x%02x",
4+i, want[i], dst[4+i]);
return 0;
}
static int
testunrle17(void)
{
/*
* Consecutive Bg runs (wasbg flag): after a Bg run sets wasbg=1, the
* next Bg run on the following scan line starts its first pixel using
* pen XOR previous-line-pixel, then copies the rest of the prev line.
* Lit 4: {0x11,0x22,0x33,0x44}; Bg 4 (copies prev → same); Bg 4.
* Third Bg (wasbg=1): pixel[8] = pen(0xFF) XOR dst[4](0x11) = 0xEE,
* pixels[9..11] copied from dst[5..7] = {0x22,0x33,0x44}.
*/
uchar src[] = {0x84, 0x11, 0x22, 0x33, 0x44, 0x04, 0x04};
uchar dst[12];
uchar *end;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 4, 1);
if(end == nil)
sysfatal("testunrle17: unexpected error: %r");
if(end - dst != 12)
sysfatal("testunrle17: length: want 12, got %d", (int)(end - dst));
if(dst[8] != 0xEE)
sysfatal("testunrle17: dst[8]: want 0xEE, got 0x%02x", dst[8]);
if(dst[9] != 0x22 || dst[10] != 0x33 || dst[11] != 0x44)
sysfatal("testunrle17: dst[9..11]: want 22 33 44, got %02x %02x %02x",
dst[9], dst[10], dst[11]);
return 0;
}
static int
testunrle18(void)
{
/*
* Overrun detection: output buffer is smaller than the decoded data.
* Lit 4 bytes into a 3-byte buffer must return nil with an error.
*/
uchar src[] = {0x84, 0xAA, 0xBB, 0xCC, 0xDD};
uchar dst[3];
uchar *end;
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end != nil)
sysfatal("testunrle18: expected nil return on overrun, got non-nil");
return 0;
}
static int
testunrle19(void)
{
/*
* Extended Bg opcode (0xF0): len taken from following 2-byte little-
* endian field. hdr=0xF0, len=4; first scan line → zero-fills 4 bytes.
*/
uchar src[] = {0xF0, 0x04, 0x00};
uchar dst[4];
uchar *end;
int i;
memset(dst, 0xFF, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle19: unexpected error: %r");
if(end - dst != 4)
sysfatal("testunrle19: length: want 4, got %d", (int)(end - dst));
for(i = 0; i < 4; i++)
if(dst[i] != 0)
sysfatal("testunrle19: dst[%d]: want 0, got 0x%02x", i, dst[i]);
return 0;
}
static int
testunrle20(void)
{
/*
* Extended Fg opcode (0xF1): len from 2-byte field.
* hdr=0xF1, len=3; pen=DWhite (0xFF), first scan line → no XOR.
* Output: 3 bytes of 0xFF.
*/
uchar src[] = {0xF1, 0x03, 0x00};
uchar dst[3];
uchar *end;
int i;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle20: unexpected error: %r");
if(end - dst != 3)
sysfatal("testunrle20: length: want 3, got %d", (int)(end - dst));
for(i = 0; i < 3; i++)
if(dst[i] != 0xFF)
sysfatal("testunrle20: dst[%d]: want 0xFF, got 0x%02x", i, dst[i]);
return 0;
}
static int
testunrle21(void)
{
/*
* Extended Lit opcode (0xF4): len from 2-byte field.
* hdr=0xF4, len=3; copies next 3 bytes verbatim.
*/
uchar src[] = {0xF4, 0x03, 0x00, 0xAA, 0xBB, 0xCC};
uchar dst[3];
uchar *end;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 10, 1);
if(end == nil)
sysfatal("testunrle21: unexpected error: %r");
if(end - dst != 3)
sysfatal("testunrle21: length: want 3, got %d", (int)(end - dst));
if(dst[0] != 0xAA || dst[1] != 0xBB || dst[2] != 0xCC)
sysfatal("testunrle21: pixels: want AA BB CC, got %02x %02x %02x",
dst[0], dst[1], dst[2]);
return 0;
}
static int
testunrle22(void)
{
/*
* Extended Fill opcode (0xF3) with pixelsize=4 (32-bpp):
* hdr=0xF3, len=2 pixels → 8 bytes; fill colour = {0x12,0x34,0x56,0x78}.
* Output: two copies of the 4-byte pixel.
*/
uchar src[] = {0xF3, 0x02, 0x00, 0x12, 0x34, 0x56, 0x78};
uchar dst[8];
uchar *end;
uchar want[] = {0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78};
int i;
memset(dst, 0, sizeof dst);
end = unrle(dst, sizeof dst, src, sizeof src, 8, 4);
if(end == nil)
sysfatal("testunrle22: unexpected error: %r");
if(end - dst != 8)
sysfatal("testunrle22: length: want 8, got %d", (int)(end - dst));
for(i = 0; i < 8; i++)
if(dst[i] != want[i])
sysfatal("testunrle22: dst[%d]: want 0x%02x, got 0x%02x",
i, want[i], dst[i]);
return 0;
}
int
rletests(void)
{
testmemfill1();
testmemfill2();
testmemfill3();
testmemfill4();
testmemxor1();
testmemxor2();
testmemxor3();
testmemxor4();
testunrle1();
testunrle2();
testunrle3();
testunrle4();
testunrle5();
testunrle6();
testunrle7();
testunrle8();
testunrle9();
testunrle10();
testunrle11();
testunrle12();
testunrle13();
testunrle14();
testunrle15();
testunrle16();
testunrle17();
testunrle18();
testunrle19();
testunrle20();
testunrle21();
testunrle22();
return 0;
}