shithub: rd

Download patch

ref: 5f08223b3c07a410e49fadebe66ca53be3314a67
parent: 34b514c97b38e01acf48c079c4be894f714eba78
author: Yaroslav K <yarikos@gmail.com>
date: Tue Mar 31 13:33:57 EDT 2026

Add tests for getcaps, uncomp, and unrle

--- a/efs_test.c
+++ b/efs_test.c
@@ -13,6 +13,8 @@
 
 int audiotests(void);
 int msgtests(void);
+int mppctests(void);
+int rletests(void);
 
 void
 testsannrq()
@@ -128,6 +130,8 @@
 	testcnrq();
 	audiotests();
 	msgtests();
+	mppctests();
+	rletests();
 	print("ok\n");
 	exits(nil);
 }
--- a/mkfile
+++ b/mkfile
@@ -34,11 +34,13 @@
 
 THREADOFILES=${OFILES:rd.$O=rd-thread.$O}
 CLEANFILES=$O.thread $O.test
-TESTHFILES=audio.c
+TESTHFILES=audio.c mppc.c rle.c
 TESTOFILES=\
 	efs_test.$O	efs.$O utf16.$O \
 	aud_test.$O	\
-	msg_test.$O	x224.$O mcs.$O ele.$O mpas.$O mppc.$O alloc.$O cap.$O egdi.$O \
+	msg_test.$O	x224.$O mcs.$O ele.$O mpas.$O alloc.$O cap.$O egdi.$O \
+	mppc_test.$O	\
+	rle_test.$O	\
 
 </sys/src/cmd/mkone
 #<$PLAN9/src/mkone
--- /dev/null
+++ b/mppc_test.c
@@ -1,0 +1,50 @@
+#include "mppc.c"
+
+int mppctests(void);
+
+static int
+testuncomp1(void)
+{
+	/* Preset with no Pcompress: history is reset and input returned unchanged */
+	uchar input[] = {0x12, 0x34};
+	int psize = -1;
+	uchar *out;
+
+	out = uncomp(input, 2, 0x80, &psize);	/* flags=Preset */
+	if(out == nil)
+		sysfatal("testuncomp1: unexpected error: %r");
+	if(out != input)
+		sysfatal("testuncomp1: expected input buffer returned unchanged");
+	if(psize != 2)
+		sysfatal("testuncomp1: psize: want 2, got %d", psize);
+	return 0;
+}
+
+static int
+testuncomp2(void)
+{
+	/*
+	 * Preset + Pcompress: decompress two 7-bit literals.
+	 * In 8K mode, bytes with high bit 0 encode as Lit7 and decode to themselves.
+	 */
+	uchar input[] = {0x41, 0x42};	/* 'A', 'B' as 7-bit literals */
+	int psize = -1;
+	uchar *out;
+
+	out = uncomp(input, 2, 0x80|0x20, &psize);	/* flags=Preset|Pcompress */
+	if(out == nil)
+		sysfatal("testuncomp2: unexpected error: %r");
+	if(psize != 2)
+		sysfatal("testuncomp2: psize: want 2, got %d", psize);
+	if(out[0] != 'A' || out[1] != 'B')
+		sysfatal("testuncomp2: data: want AB, got %c%c", out[0], out[1]);
+	return 0;
+}
+
+int
+mppctests(void)
+{
+	testuncomp1();
+	testuncomp2();
+	return 0;
+}
--- a/msg_test.c
+++ b/msg_test.c
@@ -865,6 +865,75 @@
 	return 0;
 }
 
+static int
+testgetcaps1(void)
+{
+	/*
+	 * Server Demand Active PDU containing two capability sets:
+	 *   CapGeneral (type=1, len=24): canrefresh=1, cansupress=1
+	 *   CapBitmap  (type=2, len=16): depth=32, xsz=1024, ysz=768
+	 * getcaps is exercised via getmsg → getshareT → getcaps.
+	 */
+	int n, nb;
+	uchar buf[128];
+	Msg m = {0};
+	Share as = {0};
+	Caps caps;
+	char *hex =
+		/* TPKT + X.224 Data */
+		"0300004902F080"
+		/* MCS Send Data Indication on GLOBALCHAN */
+		"68" "1111" "03EB" "70" "803A"
+		/* Share Control Header: totalLength=58, PDUactivate, source */
+		"3A00" "0100" "1111"
+		/* shareid(LE), nsrc=0, capsLen=44 */
+		"EFBEADDE" "0000" "2C00"
+		/* ncap=2, pad */
+		"02000000"
+		/* CapGeneral: type=1, len=24; canrefresh=p[22]=1, cansupress=p[23]=1 */
+		"01001800"
+		"00000000000000000000"	// data[4..13]
+		"0000000000000000"	// data[14..21]
+		"0101"			// canrefresh, cansupress
+		/* CapBitmap: type=2, len=16; depth=32, xsz=1024, ysz=768 */
+		"02001000"
+		"2000"			// depth=32
+		"000000000000"		// receive* fields
+		"00040003";		// xsz=1024 (LE), ysz=768 (LE)
+
+	nb = dec16(buf, sizeof buf, hex, strlen(hex));
+	n = getmsg(&m, buf, nb);
+	if(n <= 0)
+		sysfatal("testgetcaps1: getmsg: unexpected error: %r");
+	if(m.type != Aupdate)
+		sysfatal("testgetcaps1: type: want Aupdate, got %d", m.type);
+	if(m.getshare != getshareT)
+		sysfatal("testgetcaps1: getshare: expected getshareT");
+	n = m.getshare(&as, m.data, m.ndata);
+	if(n <= 0)
+		sysfatal("testgetcaps1: getshare: unexpected error: %r");
+	if(as.type != ShActivate)
+		sysfatal("testgetcaps1: share type: want ShActivate, got %d", as.type);
+	n = getcaps(&caps, as.data, as.ndata);
+	if(n < 0)
+		sysfatal("testgetcaps1: getcaps: unexpected error: %r");
+	if(!caps.general)
+		sysfatal("testgetcaps1: caps.general: want 1, got 0");
+	if(caps.canrefresh != 1)
+		sysfatal("testgetcaps1: canrefresh: want 1, got %d", caps.canrefresh);
+	if(caps.cansupress != 1)
+		sysfatal("testgetcaps1: cansupress: want 1, got %d", caps.cansupress);
+	if(!caps.bitmap)
+		sysfatal("testgetcaps1: caps.bitmap: want 1, got 0");
+	if(caps.depth != 32)
+		sysfatal("testgetcaps1: depth: want 32, got %d", caps.depth);
+	if(caps.xsz != 1024)
+		sysfatal("testgetcaps1: xsz: want 1024, got %d", caps.xsz);
+	if(caps.ysz != 768)
+		sysfatal("testgetcaps1: ysz: want 768, got %d", caps.ysz);
+	return 0;
+}
+
 int
 msgtests(void)
 {
@@ -905,5 +974,6 @@
 	testgetmsg16();
 	testgetmsg17();
 	testgetmsg18();
+	testgetcaps1();
 	return 0;
 }
--- /dev/null
+++ b/rle_test.c
@@ -1,0 +1,78 @@
+#include "rle.c"
+
+int rletests(void);
+
+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;
+}
+
+int
+rletests(void)
+{
+	testunrle1();
+	testunrle2();
+	testunrle3();
+	return 0;
+}
--