TestStrings.Mod 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. MODULE TestStrings; (** AUTHOR "staubesv"; PURPOSE "Testbed for Strings.Mod"; *)
  2. IMPORT
  3. SYSTEM,
  4. Commands, Strings, KernelLog;
  5. CONST
  6. Step = MAX(LONGINT) DIV 5;
  7. PROCEDURE TestIntegerConversion*(context : Commands.Context);
  8. VAR i, temp : LONGINT; string : ARRAY 16 OF CHAR;
  9. BEGIN
  10. context.out.String("Test integer <-> string conversion... "); context.out.Update;
  11. i := MIN(LONGINT);
  12. WHILE (i < MAX(LONGINT)) DO
  13. Strings.IntToStr(i, string);
  14. Strings.StrToInt(string, temp);
  15. ASSERT(i = temp);
  16. INC(i);
  17. IF (i MOD Step = 0) THEN
  18. IF (i < 0) THEN
  19. context.out.Int(ENTIER(100* ((MAX(LONGINT) + i) / MAX(LONGINT) / 2)), 0);
  20. ELSE
  21. context.out.Int(ENTIER(100* (i / MAX(LONGINT) / 2)) + 50, 0);
  22. END;
  23. context.out.String("% "); context.out.Update;
  24. END;
  25. END;
  26. Strings.IntToStr(MAX(LONGINT), string);
  27. Strings.StrToInt(string, temp);
  28. ASSERT(temp = MAX(LONGINT));
  29. context.out.String("100% done."); context.out.Ln;
  30. END TestIntegerConversion;
  31. PROCEDURE TestHexConversion*(context : Commands.Context);
  32. VAR i, val : LONGINT; res: WORD; string : ARRAY 16 OF CHAR;
  33. BEGIN
  34. context.out.String("Test hex <-> string conversion... "); context.out.Update;
  35. i := MIN(LONGINT);
  36. WHILE (i < MAX(LONGINT)) DO
  37. Strings.IntToHexStr(i, 8, string);
  38. Strings.HexStrToInt(string, val, res);
  39. IF (res # Strings.Ok) OR (i # val) THEN
  40. context.out.String("Error for string "); context.out.String(string); context.out.Ln;
  41. END;
  42. ASSERT((res = Strings.Ok) & (i = val));
  43. INC(i);
  44. IF (i MOD Step = 0) THEN
  45. IF (i < 0) THEN
  46. context.out.Int(ENTIER(100* ((MAX(LONGINT) + i) / MAX(LONGINT) / 2)), 0);
  47. ELSE
  48. context.out.Int(ENTIER(100* (i / MAX(LONGINT) / 2)) + 50, 0);
  49. END;
  50. context.out.String("% "); context.out.Update;
  51. END;
  52. END;
  53. Strings.IntToHexStr(i, 8, string);
  54. Strings.HexStrToInt(string, val, res);
  55. ASSERT((res = Strings.Ok) & (i = val));
  56. context.out.String("100% done."); context.out.Ln;
  57. END TestHexConversion;
  58. PROCEDURE TestNegativeHexConversion*(context : Commands.Context);
  59. VAR i : LONGINT;
  60. PROCEDURE Test(number : HUGEINT);
  61. VAR string, signedString : ARRAY 16 OF CHAR; val, idx, res : LONGINT;
  62. BEGIN
  63. Strings.IntToHexStr(number, 8, string);
  64. signedString[0] := "-";
  65. idx := 0; WHILE (string[idx] # 0X) DO signedString[idx + 1] := string[idx]; INC(idx); END;
  66. signedString[idx + 1] := 0X;
  67. Strings.HexStrToInt(signedString, val, res);
  68. IF (res # Strings.Ok) OR (-i # val) THEN
  69. context.out.String("Error for string "); context.out.String(string); context.out.Ln;
  70. END;
  71. ASSERT((res = Strings.Ok) & (-i = val));
  72. END Test;
  73. PROCEDURE TestMaxLongintPlus1;
  74. VAR string : ARRAY 16 OF CHAR; val : LONGINT; res: WORD;
  75. BEGIN
  76. string := "-80000000";
  77. Strings.HexStrToInt(string, val, res);
  78. ASSERT((res = Strings.Ok) & (val = MIN(LONGINT)));
  79. END TestMaxLongintPlus1;
  80. BEGIN
  81. context.out.String("Test negative hex <-> string conversion... "); context.out.Update;
  82. i := 0;
  83. WHILE (i < MAX(LONGINT)) DO
  84. Test(i);
  85. INC(i);
  86. IF (i MOD Step = 0) THEN
  87. IF (i < 0) THEN
  88. context.out.Int(ENTIER(100* ((MAX(LONGINT) + i) / MAX(LONGINT) / 2)), 0);
  89. ELSE
  90. context.out.Int(ENTIER(100* (i / MAX(LONGINT) / 2)) + 50, 0);
  91. END;
  92. context.out.String("% "); context.out.Update;
  93. END;
  94. END;
  95. Test(MAX(LONGINT));
  96. TestMaxLongintPlus1;
  97. context.out.String("100% done."); context.out.Ln;
  98. END TestNegativeHexConversion;
  99. PROCEDURE TestSetConversion*(context : Commands.Context);
  100. VAR i : LONGINT; temp : SET; string : ARRAY 64 OF CHAR;
  101. BEGIN
  102. context.out.String("Test set <-> string conversion... "); context.out.Update;
  103. i := MIN(LONGINT);
  104. WHILE (i < MAX(LONGINT)) DO
  105. Strings.SetToStr(SYSTEM.VAL(SET, i), string);
  106. Strings.StrToSet(string, temp);
  107. IF (SYSTEM.VAL(SET, i) # temp) THEN
  108. KernelLog.Bits(SYSTEM.VAL(SET, i), 0, 32); KernelLog.String(" # "); KernelLog.Bits(temp, 0, 32); KernelLog.Ln;
  109. END;
  110. ASSERT(SYSTEM.VAL(SET, i) = temp);
  111. INC(i);
  112. IF (i MOD Step = 0) THEN
  113. IF (i < 0) THEN
  114. context.out.Int(ENTIER(100* ((MAX(LONGINT) + i) / MAX(LONGINT) / 2)), 0);
  115. ELSE
  116. context.out.Int(ENTIER(100* (i / MAX(LONGINT) / 2)) + 50, 0);
  117. END;
  118. context.out.String("% "); context.out.Update;
  119. END;
  120. END;
  121. Strings.SetToStr(SYSTEM.VAL(SET, MAX(LONGINT)), string);
  122. Strings.StrToSet(string, temp);
  123. ASSERT(SYSTEM.VAL(SET, MAX(LONGINT)) = temp);
  124. context.out.String("100% done."); context.out.Ln;
  125. END TestSetConversion;
  126. PROCEDURE TestSplitJoin*(context : Commands.Context);
  127. VAR string : ARRAY 1024 OF CHAR; separator : CHAR; sa : Strings.StringArray; s : Strings.String; i : LONGINT;
  128. BEGIN
  129. separator := 0X;
  130. context.arg.SkipWhitespace; context.arg.String(string);
  131. context.arg.SkipWhitespace; context.arg.Char(separator);
  132. context.out.String("String: '"); context.out.String(string); context.out.String("', separator: ");
  133. IF (separator # 0X) THEN context.out.Char(separator); ELSE context.out.String("none"); END;
  134. context.out.Ln;
  135. sa := Strings.Split(string, separator);
  136. FOR i := 0 TO LEN(sa)-1 DO
  137. context.out.Int(i, 2); context.out.String(": ");
  138. context.out.String(sa[i]^);
  139. context.out.Ln;
  140. END;
  141. s := Strings.Join(sa, 0, LEN(sa)-1, separator);
  142. context.out.String("Joined string: '"); context.out.String(s^); context.out.String("'"); context.out.Ln;
  143. context.out.String("Success: ");
  144. IF (s^ = string) THEN context.out.String("Yes"); ELSE context.out.String("No"); END;
  145. context.out.Ln;
  146. END TestSplitJoin;
  147. PROCEDURE PerformTests*(context : Commands.Context);
  148. BEGIN
  149. TestIntegerConversion(context);
  150. TestHexConversion(context);
  151. TestNegativeHexConversion(context);
  152. TestSetConversion(context);
  153. END PerformTests;
  154. END TestStrings.
  155. TestStrings.TestIntegerConversion ~
  156. TestStrings.TestHexConversion ~
  157. TestStrings.TestNegativeHexConversion ~
  158. TestStrings.TestSetConversion ~
  159. TestStrings.TestSplitJoin A:/Test/HelloWord/Test.Mod / ~
  160. TestStrings.PerformTests ~
  161. System.Free TestStrings ~