| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- MODULE Showcase16;
- // Session showcase: variant records, UString assignment to an
- // ARRAY OF UCHAR, and Files layered on IOChan.
- //
- // It builds a Shape variant record, encodes a Unicode label into a
- // UCHAR array, writes both through a Files handle (itself an IOChan
- // channel), reads the line back and prints a summary.
- // Expected ExitCode: 42.
- IMPORT Files, TextIO, Strings, Conversions;
- TYPE
- Kind = (circle, square, rect);
- Shape = RECORD
- id : INTEGER;
- CASE k : Kind OF
- circle : radius : INTEGER;
- | square : side : INTEGER;
- | rect : w, h : INTEGER;
- END;
- END;
- VAR ExitCode : INTEGER;
- VAR f : Files.File;
- VAR name, line : ARRAY [0 .. 63] OF CHAR;
- VAR label : ARRAY [0 .. 7] OF UCHAR;
- VAR s : Shape;
- VAR total : INTEGER;
- BEGIN
- ExitCode := 0;
- (* variant record: the rect branch's fields overlay one slot *)
- s.id := 3;
- s.k := rect;
- s.w := 20;
- s.h := 22; (* shares the slot with s.w *)
- IF s.id = 3 THEN ExitCode := ExitCode + 1 ELSE ExitCode := 90 END;
- IF s.k = rect THEN ExitCode := ExitCode + 2 ELSE ExitCode := 91 END;
- IF s.h = 22 THEN ExitCode := ExitCode + 4 ELSE ExitCode := 95 END;
- total := s.id * s.h - 24; (* 3*22 - 24 = 42 *)
- (* UString assignment into a UCHAR array (content copy) *)
- label := U"ok✓";
- IF label[0] = U'o' THEN ExitCode := ExitCode + 8 ELSE ExitCode := 92 END;
- IF label[2] = U'✓' THEN ExitCode := ExitCode + 16 ELSE ExitCode := 93 END;
- (* write through Files (an IOChan channel), then read back *)
- Strings.Assign("gen_ssa/_showcase16.txt", name);
- f := Files.OpenWrite(name);
- Files.WriteString(f, "id=");
- Files.WriteInt(f, s.id);
- Files.WriteString(f, " total=");
- Files.WriteInt(f, total);
- Files.WriteLn(f);
- Files.Close(f);
- f := Files.OpenRead(name);
- Files.ReadString(f, line);
- IF line = "id=3 total=42" THEN
- ExitCode := ExitCode + 32
- ELSE ExitCode := 94 END;
- Files.Close(f);
- TextIO.WriteString("showcase16: ");
- TextIO.WriteString(line);
- TextIO.WriteLn;
- IF ExitCode = 63 THEN ExitCode := total END (* 42 *)
- END Showcase16.
|