showcase16.mod 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. MODULE Showcase16;
  2. // Session showcase: variant records, UString assignment to an
  3. // ARRAY OF UCHAR, and Files layered on IOChan.
  4. //
  5. // It builds a Shape variant record, encodes a Unicode label into a
  6. // UCHAR array, writes both through a Files handle (itself an IOChan
  7. // channel), reads the line back and prints a summary.
  8. // Expected ExitCode: 42.
  9. IMPORT Files, TextIO, Strings, Conversions;
  10. TYPE
  11. Kind = (circle, square, rect);
  12. Shape = RECORD
  13. id : INTEGER;
  14. CASE k : Kind OF
  15. circle : radius : INTEGER;
  16. | square : side : INTEGER;
  17. | rect : w, h : INTEGER;
  18. END;
  19. END;
  20. VAR ExitCode : INTEGER;
  21. VAR f : Files.File;
  22. VAR name, line : ARRAY [0 .. 63] OF CHAR;
  23. VAR label : ARRAY [0 .. 7] OF UCHAR;
  24. VAR s : Shape;
  25. VAR total : INTEGER;
  26. BEGIN
  27. ExitCode := 0;
  28. (* variant record: the rect branch's fields overlay one slot *)
  29. s.id := 3;
  30. s.k := rect;
  31. s.w := 20;
  32. s.h := 22; (* shares the slot with s.w *)
  33. IF s.id = 3 THEN ExitCode := ExitCode + 1 ELSE ExitCode := 90 END;
  34. IF s.k = rect THEN ExitCode := ExitCode + 2 ELSE ExitCode := 91 END;
  35. IF s.h = 22 THEN ExitCode := ExitCode + 4 ELSE ExitCode := 95 END;
  36. total := s.id * s.h - 24; (* 3*22 - 24 = 42 *)
  37. (* UString assignment into a UCHAR array (content copy) *)
  38. label := U"ok✓";
  39. IF label[0] = U'o' THEN ExitCode := ExitCode + 8 ELSE ExitCode := 92 END;
  40. IF label[2] = U'✓' THEN ExitCode := ExitCode + 16 ELSE ExitCode := 93 END;
  41. (* write through Files (an IOChan channel), then read back *)
  42. Strings.Assign("gen_ssa/_showcase16.txt", name);
  43. f := Files.OpenWrite(name);
  44. Files.WriteString(f, "id=");
  45. Files.WriteInt(f, s.id);
  46. Files.WriteString(f, " total=");
  47. Files.WriteInt(f, total);
  48. Files.WriteLn(f);
  49. Files.Close(f);
  50. f := Files.OpenRead(name);
  51. Files.ReadString(f, line);
  52. IF line = "id=3 total=42" THEN
  53. ExitCode := ExitCode + 32
  54. ELSE ExitCode := 94 END;
  55. Files.Close(f);
  56. TextIO.WriteString("showcase16: ");
  57. TextIO.WriteString(line);
  58. TextIO.WriteLn;
  59. IF ExitCode = 63 THEN ExitCode := total END (* 42 *)
  60. END Showcase16.