6 changed files
src/Morfologik.TestFramework | ||
RandomizedTesting/Generators | ||
AsciiAlphanumGenerator.cs | ||
AsciiLettersGenerator.cs | ||
CodepointSetGenerator.cs | ||
RandomNumbers.cs | ||
Morfologik.TestFramework.csproj | ||
TestCase.cs | ||
AsciiAlphanumGenerator.cs
/src/Morfologik.TestFramework/RandomizedTesting/Generators/AsciiAlphanumGenerator.cs-28/src/Morfologik.TestFramework/RandomizedTesting/Generators/AsciiAlphanumGenerator.cs
Add comment 1 Minus //using System;
Add comment 2 Minus //using System.Collections.Generic;
Add comment 3 Minus //using System.Text;
Add comment 4 Minus
Add comment 5 Minus //namespace Morfologik.TestFramework.RandomizedTesting.Generators
Add comment 6 Minus //{
Add comment 7 Minus // /// <summary>
Add comment 8 Minus // /// A generator emitting simple ASCII alphanumeric letters and numbers
Add comment 9 Minus // /// from the set (newlines not counted):
Add comment 10 Minus // /// <para/>
Add comment 11 Minus // /// abcdefghijklmnopqrstuvwxyz
Add comment 12 Minus // /// ABCDEFGHIJKLMNOPQRSTUVWXYZ
Add comment 13 Minus // /// 0123456789
Add comment 14 Minus // /// </summary>
Add comment 15 Minus // public class AsciiAlphanumGenerator : CodepointSetGenerator
Add comment 16 Minus // {
Add comment 17 Minus // private readonly static char[] CHARS =
Add comment 18 Minus // ("abcdefghijklmnopqrstuvwxyz" +
Add comment 19 Minus // "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
Add comment 20 Minus // "0123456789").ToCharArray();
Add comment 21 Minus
Add comment 22 Minus // public AsciiAlphanumGenerator()
Add comment 23 Minus // : base(CHARS)
Add comment 24 Minus // {
Add comment 25 Minus // }
Add comment 26 Minus // }
Add comment 27 Minus //}
Add comment 28 Minus
AsciiLettersGenerator.cs
/src/Morfologik.TestFramework/RandomizedTesting/Generators/AsciiLettersGenerator.cs-23/src/Morfologik.TestFramework/RandomizedTesting/Generators/AsciiLettersGenerator.cs
Add comment 1 Minus namespace Morfologik.TestFramework.RandomizedTesting.Generators
Add comment 2 Minus {
Add comment 3 Minus /// <summary>
Add comment 4 Minus /// A generator emitting simple ASCII characters from the set
Add comment 5 Minus /// (newlines not counted):
Add comment 6 Minus /// <code>
Add comment 7 Minus /// abcdefghijklmnopqrstuvwxyz
Add comment 8 Minus /// ABCDEFGHIJKLMNOPQRSTUVWXYZ
Add comment 9 Minus /// </code>
Add comment 10 Minus /// </summary>
Add comment 11 Minus public class AsciiLettersGenerator : CodepointSetGenerator
Add comment 12 Minus {
Add comment 13 Minus private readonly static char[] Chars =
Add comment 14 Minus ("abcdefghijklmnopqrstuvwxyz" +
Add comment 15 Minus "ABCDEFGHIJKLMNOPQRSTUVWXYZ").ToCharArray();
Add comment 16 Minus
Add comment 17 Minus public AsciiLettersGenerator()
Add comment 18 Minus : base(Chars)
Add comment 19 Minus {
Add comment 20 Minus }
Add comment 21 Minus }
Add comment 22 Minus }
Add comment 23 Minus
CodepointSetGenerator.cs
/src/Morfologik.TestFramework/RandomizedTesting/Generators/CodepointSetGenerator.cs-265/src/Morfologik.TestFramework/RandomizedTesting/Generators/CodepointSetGenerator.cs
Add comment 1 Minus using J2N;
Add comment 2 Minus using System;
Add comment 3 Minus using System.Collections.Generic;
Add comment 4 Minus using System.Text;
Add comment 5 Minus
Add comment 6 Minus namespace Morfologik.TestFramework.RandomizedTesting.Generators
Add comment 7 Minus {
Add comment 8 Minus /// <summary>
Add comment 9 Minus /// A string generator from a predefined set of codepoints or characters.
Add comment 10 Minus /// </summary>
Add comment 11 Minus public class CodepointSetGenerator //: StringGenerator
Add comment 12 Minus {
Add comment 13 Minus private readonly int[] bmp;
Add comment 14 Minus private readonly int[] supplementary;
Add comment 15 Minus private readonly int[] all;
Add comment 16 Minus
Add comment 17 Minus /**
Add comment 18 Minus * All characters must be from BMP (no parts of surrogate pairs allowed).
Add comment 19 Minus */
Add comment 20 Minus public CodepointSetGenerator(char[] chars)
Add comment 21 Minus {
Add comment 22 Minus this.bmp = new int[chars.Length];
Add comment 23 Minus this.supplementary = new int[0];
Add comment 24 Minus
Add comment 25 Minus for (int i = 0; i < chars.Length; i++)
Add comment 26 Minus {
Add comment 27 Minus bmp[i] = ((int)chars[i]) & 0xffff;
Add comment 28 Minus
Add comment 29 Minus if (IsSurrogate(chars[i]))
Add comment 30 Minus {
Add comment 31 Minus throw new ArgumentException("Value is part of a surrogate pair: 0x"
Add comment 32 Minus + bmp[i].ToHexString());
Add comment 33 Minus }
Add comment 34 Minus }
Add comment 35 Minus
Add comment 36 Minus this.all = Concat(bmp, supplementary);
Add comment 37 Minus if (all.Length == 0)
Add comment 38 Minus {
Add comment 39 Minus throw new ArgumentException("Empty set of characters?");
Add comment 40 Minus }
Add comment 41 Minus }
Add comment 42 Minus
Add comment 43 Minus /**
Add comment 44 Minus * Parse the given {@link String} and split into BMP and supplementary codepoints.
Add comment 45 Minus */
Add comment 46 Minus public CodepointSetGenerator(string s)
Add comment 47 Minus {
Add comment 48 Minus int bmps = 0;
Add comment 49 Minus int supplementaries = 0;
Add comment 50 Minus for (int i = 0; i < s.Length;)
Add comment 51 Minus {
Add comment 52 Minus int codepoint = s.CodePointAt(i);
Add comment 53 Minus if (/*Character.*/IsSupplementaryCodePoint(codepoint))
Add comment 54 Minus {
Add comment 55 Minus supplementaries++;
Add comment 56 Minus }
Add comment 57 Minus else
Add comment 58 Minus {
Add comment 59 Minus bmps++;
Add comment 60 Minus }
Add comment 61 Minus
Add comment 62 Minus i += /*Character.*/CharCount(codepoint);
Add comment 63 Minus }
Add comment 64 Minus
Add comment 65 Minus this.bmp = new int[bmps];
Add comment 66 Minus this.supplementary = new int[supplementaries];
Add comment 67 Minus for (int i = 0; i < s.Length;)
Add comment 68 Minus {
Add comment 69 Minus int codepoint = s.CodePointAt(i);
Add comment 70 Minus if (/*Character.*/IsSupplementaryCodePoint(codepoint))
Add comment 71 Minus {
Add comment 72 Minus supplementary[--supplementaries] = codepoint;
Add comment 73 Minus }
Add comment 74 Minus else
Add comment 75 Minus {
Add comment 76 Minus bmp[--bmps] = codepoint;
Add comment 77 Minus }
Add comment 78 Minus
Add comment 79 Minus i += /*Character.*/CharCount(codepoint);
Add comment 80 Minus }
Add comment 81 Minus
Add comment 82 Minus this.all = Concat(bmp, supplementary);
Add comment 83 Minus if (all.Length == 0)
Add comment 84 Minus {
Add comment 85 Minus throw new ArgumentException("Empty set of characters?");
Add comment 86 Minus }
Add comment 87 Minus }
Add comment 88 Minus
Add comment 89 Minus public /*override*/ string OfCodeUnitsLength(System.Random r, int minCodeUnits, int maxCodeUnits)
Add comment 90 Minus {
Add comment 91 Minus int length = RandomNumbers.RandomInt32Between(r, minCodeUnits, maxCodeUnits);
Add comment 92 Minus
Add comment 93 Minus // Check and cater for odd number of code units if no bmp characters are given.
Add comment 94 Minus if (bmp.Length == 0 && IsOdd(length))
Add comment 95 Minus {
Add comment 96 Minus if (minCodeUnits == maxCodeUnits)
Add comment 97 Minus {
Add comment 98 Minus throw new ArgumentException("Cannot return an odd number of code units "
Add comment 99 Minus + " when surrogate pairs are the only available codepoints.");
Add comment 100 Minus }
Add comment 101 Minus else
Add comment 102 Minus {
Add comment 103 Minus // length is odd so we move forward or backward to the closest even number.
Add comment 104 Minus if (length == minCodeUnits)
Add comment 105 Minus {
Add comment 106 Minus length++;
Add comment 107 Minus }
Add comment 108 Minus else
Add comment 109 Minus {
Add comment 110 Minus length--;
Add comment 111 Minus }
Add comment 112 Minus }
Add comment 113 Minus }
Add comment 114 Minus
Add comment 115 Minus //int[] codepoints = new int[length];
Add comment 116 Minus char[] chars = new char[length * 2];
Add comment 117 Minus int actual = 0;
Add comment 118 Minus while (length > 0)
Add comment 119 Minus {
Add comment 120 Minus int cp;
Add comment 121 Minus if (length == 1)
Add comment 122 Minus {
Add comment 123 Minus //codepoints[actual] = bmp[r.Next(bmp.Length)];
Add comment 124 Minus cp = bmp[r.Next(bmp.Length)];
Add comment 125 Minus }
Add comment 126 Minus else
Add comment 127 Minus {
Add comment 128 Minus //codepoints[actual] = all[r.Next(all.Length)];
Add comment 129 Minus cp = all[r.Next(all.Length)];
Add comment 130 Minus }
Add comment 131 Minus char[] temp = ToChars(cp);
Add comment 132 Minus for (int i = 0; i < temp.Length; i++)
Add comment 133 Minus chars[actual++] = temp[i];
Add comment 134 Minus
Add comment 135 Minus //if (/*Character.*/IsSupplementaryCodePoint(codepoints[actual]))
Add comment 136 Minus if (/*Character.*/IsSupplementaryCodePoint(cp))
Add comment 137 Minus {
Add comment 138 Minus length -= 2;
Add comment 139 Minus }
Add comment 140 Minus else
Add comment 141 Minus {
Add comment 142 Minus length -= 1;
Add comment 143 Minus }
Add comment 144 Minus //actual++;
Add comment 145 Minus }
Add comment 146 Minus //return new string(codepoints, 0, actual);
Add comment 147 Minus return new string(chars, 0, actual);
Add comment 148 Minus }
Add comment 149 Minus
Add comment 150 Minus //public override string OfCodePointsLength(System.Random r, int minCodePoints, int maxCodePoints)
Add comment 151 Minus //{
Add comment 152 Minus // int length = RandomNumbers.RandomInt32Between(r, minCodePoints, maxCodePoints);
Add comment 153 Minus // int[] codepoints = new int[length];
Add comment 154 Minus // while (length > 0)
Add comment 155 Minus // {
Add comment 156 Minus // codepoints[--length] = all[r.Next(all.Length)];
Add comment 157 Minus // }
Add comment 158 Minus // return new string(codepoints, 0, codepoints.Length);
Add comment 159 Minus //}
Add comment 160 Minus
Add comment 161 Minus /** Is a given number odd? */
Add comment 162 Minus private static bool IsOdd(int v)
Add comment 163 Minus {
Add comment 164 Minus return (v & 1) != 0;
Add comment 165 Minus }
Add comment 166 Minus
Add comment 167 Minus private int[] Concat(params int[][] arrays)
Add comment 168 Minus {
Add comment 169 Minus int totalLength = 0;
Add comment 170 Minus foreach (int[] a in arrays) totalLength += a.Length;
Add comment 171 Minus int[] concat = new int[totalLength];
Add comment 172 Minus for (int i = 0, j = 0; j < arrays.Length;)
Add comment 173 Minus {
Add comment 174 Minus System.Array.Copy(arrays[j], 0, concat, i, arrays[j].Length);
Add comment 175 Minus i += arrays[j].Length;
Add comment 176 Minus j++;
Add comment 177 Minus }
Add comment 178 Minus return concat;
Add comment 179 Minus }
Add comment 180 Minus
Add comment 181 Minus private bool IsSurrogate(char chr)
Add comment 182 Minus {
Add comment 183 Minus return (chr >= 0xd800 && chr <= 0xdfff);
Add comment 184 Minus }
Add comment 185 Minus
Add comment 186 Minus #region From Character Class
Add comment 187 Minus
Add comment 188 Minus internal const int MaxCodePoint = 0x10FFFF;
Add comment 189 Minus internal const int MinCodePoint = 0x000000;
Add comment 190 Minus internal const int MinSupplementaryCodePoint = 0x010000;
Add comment 191 Minus
Add comment 192 Minus internal const char MinLowSurrogate = '\uDC00';
Add comment 193 Minus internal const char MaxLowSurrogate = '\uDFFF';
Add comment 194 Minus
Add comment 195 Minus internal const char MinHighSurrogate = '\uD800';
Add comment 196 Minus internal const char MaxHighSurrogate = '\uDBFF';
Add comment 197 Minus
Add comment 198 Minus private static int CharCount(int codePoint)
Add comment 199 Minus {
Add comment 200 Minus // A given codepoint can be represented in .NET either by 1 char (up to UTF16),
Add comment 201 Minus // or by if it's a UTF32 codepoint, in which case the current char will be a surrogate
Add comment 202 Minus return codePoint >= MinSupplementaryCodePoint ? 2 : 1;
Add comment 203 Minus }
Add comment 204 Minus private static bool IsSupplementaryCodePoint(int codePoint)
Add comment 205 Minus {
Add comment 206 Minus return (MinSupplementaryCodePoint <= codePoint && MaxCodePoint >= codePoint);
Add comment 207 Minus }
Add comment 208 Minus
Add comment 209 Minus private static bool IsValidCodePoint(int codePoint)
Add comment 210 Minus {
Add comment 211 Minus return (MinCodePoint <= codePoint && MaxCodePoint >= codePoint);
Add comment 212 Minus }
Add comment 213 Minus
Add comment 214 Minus private static char[] ToChars(int codePoint)
Add comment 215 Minus {
Add comment 216 Minus if (!IsValidCodePoint(codePoint))
Add comment 217 Minus {
Add comment 218 Minus throw new ArgumentException();
Add comment 219 Minus }
Add comment 220 Minus
Add comment 221 Minus if (IsSupplementaryCodePoint(codePoint))
Add comment 222 Minus {
Add comment 223 Minus int cpPrime = codePoint - 0x10000;
Add comment 224 Minus int high = 0xD800 | ((cpPrime >> 10) & 0x3FF);
Add comment 225 Minus int low = 0xDC00 | (cpPrime & 0x3FF);
Add comment 226 Minus return new char[] { (char)high, (char)low };
Add comment 227 Minus }
Add comment 228 Minus return new char[] { (char)codePoint };
Add comment 229 Minus }
Add comment 230 Minus
Add comment 231 Minus #endregion
Add comment 232 Minus }
Add comment 233 Minus
Add comment 234 Minus internal static class ExtensionMethods
Add comment 235 Minus {
Add comment 236 Minus public static int CodePointAt(this string seq, int index)
Add comment 237 Minus {
Add comment 238 Minus char c1 = seq[index++];
Add comment 239 Minus if (char.IsHighSurrogate(c1))
Add comment 240 Minus {
Add comment 241 Minus if (index < seq.Length)
Add comment 242 Minus {
Add comment 243 Minus char c2 = seq[index];
Add comment 244 Minus if (char.IsLowSurrogate(c2))
Add comment 245 Minus {
Add comment 246 Minus return ToCodePoint(c1, c2);
Add comment 247 Minus }
Add comment 248 Minus }
Add comment 249 Minus }
Add comment 250 Minus return c1;
Add comment 251 Minus }
Add comment 252 Minus
Add comment 253 Minus public static int ToCodePoint(char high, char low)
Add comment 254 Minus {
Add comment 255 Minus // Optimized form of:
Add comment 256 Minus // return ((high - MIN_HIGH_SURROGATE) << 10)
Add comment 257 Minus // + (low - MIN_LOW_SURROGATE)
Add comment 258 Minus // + MIN_SUPPLEMENTARY_CODE_POINT;
Add comment 259 Minus return ((high << 10) + low) + (CodepointSetGenerator.MinSupplementaryCodePoint
Add comment 260 Minus - (CodepointSetGenerator.MinHighSurrogate << 10)
Add comment 261 Minus - CodepointSetGenerator.MinLowSurrogate);
Add comment 262 Minus }
Add comment 263 Minus }
Add comment 264 Minus }
Add comment 265 Minus
RandomNumbers.cs
/src/Morfologik.TestFramework/RandomizedTesting/Generators/RandomNumbers.cs-121/src/Morfologik.TestFramework/RandomizedTesting/Generators/RandomNumbers.cs
Add comment 1 Minus using System;
Add comment 2 Minus using System.Collections.Generic;
Add comment 3 Minus using System.Diagnostics;
Add comment 4 Minus using System.Text;
Add comment 5 Minus
Add comment 6 Minus namespace Morfologik.TestFramework.RandomizedTesting.Generators
Add comment 7 Minus {
Add comment 8 Minus /// <summary>
Add comment 9 Minus /// Utility classes for selecting random numbers from within a range or the
Add comment 10 Minus /// numeric domain for a given type.
Add comment 11 Minus /// </summary>
Add comment 12 Minus /// <seealso cref="BiasedNumbers"/>
Add comment 13 Minus public static class RandomNumbers
Add comment 14 Minus {
Add comment 15 Minus /// <summary>
Add comment 16 Minus /// A random integer from <paramref name="min"/> to <paramref name="max"/> (inclusive).
Add comment 17 Minus /// </summary>
Add comment 18 Minus public static int RandomInt32Between(Random random, int min, int max)
Add comment 19 Minus {
Add comment 20 Minus Debug.Assert(min <= max, String.Format("Min must be less than or equal max int. min: {0}, max: {1}", min, max));
Add comment 21 Minus var range = max - min;
Add comment 22 Minus if (range < Int32.MaxValue)
Add comment 23 Minus return min + random.Next(1 + range);
Add comment 24 Minus
Add comment 25 Minus return min + (int)Math.Round(random.NextDouble() * range);
Add comment 26 Minus }
Add comment 27 Minus
Add comment 28 Minus /* .NET has random.Next(max) which negates the need for randomInt(Random random, int max) as */
Add comment 29 Minus
Add comment 30 Minus // /**
Add comment 31 Minus //* A random integer between <code>min</code> (inclusive) and <code>max</code> (inclusive).
Add comment 32 Minus //*/
Add comment 33 Minus // public static int RandomIntBetween(Random r, int min, int max)
Add comment 34 Minus // {
Add comment 35 Minus // Debug.Assert(max >= min, "max must be >= min: " + min + ", " + max);
Add comment 36 Minus // long range = (long)max - (long)min;
Add comment 37 Minus // if (range < int.MaxValue)
Add comment 38 Minus // {
Add comment 39 Minus // return min + r.Next(1 + (int)range);
Add comment 40 Minus // }
Add comment 41 Minus // else
Add comment 42 Minus // {
Add comment 43 Minus // return ToIntExact(min + NextLong(r, 1 + range));
Add comment 44 Minus // }
Add comment 45 Minus // }
Add comment 46 Minus
Add comment 47 Minus // /**
Add comment 48 Minus // * A random long between <code>min</code> (inclusive) and <code>max</code> (inclusive).
Add comment 49 Minus // */
Add comment 50 Minus // public static long TandomLongBetween(Random r, long min, long max)
Add comment 51 Minus // {
Add comment 52 Minus // Debug.Assert(max >= min , "max must be >= min: " + min + ", " + max);
Add comment 53 Minus // long range = max - min;
Add comment 54 Minus // if (range < 0)
Add comment 55 Minus // {
Add comment 56 Minus // range -= long.MaxValue;
Add comment 57 Minus // if (range == long.MinValue)
Add comment 58 Minus // {
Add comment 59 Minus // // Full spectrum.
Add comment 60 Minus // return r.NextInt64();
Add comment 61 Minus // }
Add comment 62 Minus // else
Add comment 63 Minus // {
Add comment 64 Minus // long first = r.NextInt64() & long.MaxValue;
Add comment 65 Minus // long second = range == long.MaxValue ? (r.NextInt64() & long.MaxValue) : NextLong(r, range + 1);
Add comment 66 Minus // return min + first + second;
Add comment 67 Minus // }
Add comment 68 Minus // }
Add comment 69 Minus // else
Add comment 70 Minus // {
Add comment 71 Minus // long second = range == long.MaxValue ? (r.NextInt64() & long.MaxValue) : NextLong(r, range + 1);
Add comment 72 Minus // return min + second;
Add comment 73 Minus // }
Add comment 74 Minus // }
Add comment 75 Minus
Add comment 76 Minus // /**
Add comment 77 Minus // * Similar to {@link Random#nextInt(int)}, but returns a long between
Add comment 78 Minus // * 0 (inclusive) and <code>n</code> (exclusive).
Add comment 79 Minus // *
Add comment 80 Minus // * @param rnd Random generator.
Add comment 81 Minus // * @param n the bound on the random number to be returned. Must be
Add comment 82 Minus // * positive.
Add comment 83 Minus // * @return Returns a random number between 0 and n-1.
Add comment 84 Minus // */
Add comment 85 Minus // public static long NextLong(Random rnd, long n)
Add comment 86 Minus // {
Add comment 87 Minus // if (n <= 0)
Add comment 88 Minus // {
Add comment 89 Minus // throw new ArgumentException("n <= 0: " + n);
Add comment 90 Minus // }
Add comment 91 Minus
Add comment 92 Minus // long value = rnd.NextInt64();
Add comment 93 Minus // long range = n - 1;
Add comment 94 Minus // if ((n & range) == 0L)
Add comment 95 Minus // {
Add comment 96 Minus // value &= range;
Add comment 97 Minus // }
Add comment 98 Minus // else
Add comment 99 Minus // {
Add comment 100 Minus // for (long u = value.TripleShift(1); u + range - (value = u % n) < 0L;)
Add comment 101 Minus // {
Add comment 102 Minus // u = rnd.NextInt64().TripleShift(1);
Add comment 103 Minus // }
Add comment 104 Minus // }
Add comment 105 Minus // return value;
Add comment 106 Minus // }
Add comment 107 Minus
Add comment 108 Minus // private static int ToIntExact(long value)
Add comment 109 Minus // {
Add comment 110 Minus // if (value > int.MaxValue)
Add comment 111 Minus // {
Add comment 112 Minus // throw new ArithmeticException("Overflow: " + value);
Add comment 113 Minus // }
Add comment 114 Minus // else
Add comment 115 Minus // {
Add comment 116 Minus // return (int)value;
Add comment 117 Minus // }
Add comment 118 Minus // }
Add comment 119 Minus }
Add comment 120 Minus }
Add comment 121 Minus
Morfologik.TestFramework.csproj
/src/Morfologik.TestFramework/Morfologik.TestFramework.csproj+1/src/Morfologik.TestFramework/Morfologik.TestFramework.csproj
Add comment 11 <ItemGroup>
Add comment 12 <PackageReference Include="J2N" Version="$(J2NPackageReferenceVersion)" />
Add comment 13 <PackageReference Include="NUnit" Version="$(NUnitPackageReferenceVersion)" />
Add comment 14 Plus <PackageReference Include="RandomizedTesting.Generators" Version="$(RandomizedTestingGeneratorsPackageReferenceVersion)" />
Add comment 14 15 </ItemGroup>
Add comment 15 16
Add comment 16 17 </Project>
TestCase.cs
/src/Morfologik.TestFramework/TestCase.cs-1+1/src/Morfologik.TestFramework/TestCase.cs
Add comment 1 Minus using Morfologik.TestFramework.RandomizedTesting.Generators;
Add comment 2 1 using NUnit.Framework;
Add comment 2 Plus using RandomizedTesting.Generators;
Add comment 3 using System;
Add comment 4 using System.Collections;
Add comment 5 using System.Collections.Generic;