-
Notifications
You must be signed in to change notification settings - Fork 639
/
TestUtil.cs
1441 lines (1313 loc) · 58 KB
/
TestUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using J2N;
using J2N.IO;
using J2N.Text;
using Lucene.Net.Codecs;
using Lucene.Net.Codecs.Lucene46;
using Lucene.Net.Codecs.PerField;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Randomized.Generators;
using Lucene.Net.Search;
using Lucene.Net.Support.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using Assert = Lucene.Net.TestFramework.Assert;
using Console = Lucene.Net.Util.SystemConsole;
using Directory = Lucene.Net.Store.Directory;
using JCG = J2N.Collections.Generic;
//#if TESTFRAMEWORK_MSTEST
//using CollectionAssert = Lucene.Net.TestFramework.Assert;
//using Assert = Lucene.Net.TestFramework.Assert;
//#elif TESTFRAMEWORK_NUNIT
//using CollectionAssert = NUnit.Framework.CollectionAssert;
//using Assert = NUnit.Framework.Assert;
//#elif TESTFRAMEWORK_XUNIT
//using CollectionAssert = Lucene.Net.TestFramework.Assert;
//using Assert = Lucene.Net.TestFramework.Assert;
//#endif
namespace Lucene.Net.Util
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// General utility methods for Lucene unit tests.
/// </summary>
public static class TestUtil // LUCENENET specific - made static rather than making a private constructor
{
/// <summary>
/// Deletes one or more files or directories (and everything underneath it).
/// </summary>
/// <exception cref="IOException">If any of the given files (or their subhierarchy files in case
/// of directories) cannot be removed.</exception>
public static void Rm(params FileSystemInfo[] locations)
{
ISet<FileSystemInfo> unremoved = Rm(new JCG.HashSet<FileSystemInfo>(), locations);
if (unremoved.Count > 0)
{
StringBuilder b = new StringBuilder("Could not remove the following files (in the order of attempts):\n");
foreach (var f in unremoved)
{
b.Append(" ")
.Append(f.FullName)
.Append("\n");
}
throw new IOException(b.ToString());
}
}
private static ISet<FileSystemInfo> Rm(ISet<FileSystemInfo> unremoved, params FileSystemInfo[] locations)
{
foreach (FileSystemInfo location in locations)
{
// LUCENENET: Refresh the state of the FileSystemInfo object so we can be sure
// the Exists property is (somewhat) current.
location.Refresh();
if (location.Exists)
{
if (location is DirectoryInfo directory)
{
// Try to delete all of the files and folders in the directory
Rm(unremoved, directory.GetFileSystemInfos());
}
try
{
location.Delete();
}
catch (Exception)
{
unremoved.Add(location);
}
}
}
return unremoved;
}
/// <summary>
/// Convenience method unzipping <paramref name="zipFileStream"/> into <paramref name="destDir"/>, cleaning up
/// <paramref name="destDir"/> first.
/// </summary>
public static void Unzip(Stream zipFileStream, DirectoryInfo destDir)
{
Rm(destDir);
destDir.Create();
using ZipArchive zip = new ZipArchive(zipFileStream);
foreach (var entry in zip.Entries)
{
// Ignore internal folders - these are tacked onto the FullName anyway
if (entry.FullName.EndsWith("/", StringComparison.Ordinal) || entry.FullName.EndsWith("\\", StringComparison.Ordinal))
{
continue;
}
using Stream input = entry.Open();
FileInfo targetFile = new FileInfo(CorrectPath(Path.Combine(destDir.FullName, entry.FullName)));
if (!targetFile.Directory.Exists)
{
targetFile.Directory.Create();
}
using Stream output = new FileStream(targetFile.FullName, FileMode.Create, FileAccess.Write);
input.CopyTo(output);
}
}
/// <summary>
/// LUCENENET specific method for normalizing file path names
/// for the current operating system.
/// </summary>
private static string CorrectPath(string input)
{
if (Path.DirectorySeparatorChar.Equals('/'))
{
return input.Replace('\\', '/');
}
return input.Replace('/', '\\');
}
public static void SyncConcurrentMerges(IndexWriter writer)
{
SyncConcurrentMerges(writer.Config.MergeScheduler);
}
public static void SyncConcurrentMerges(IMergeScheduler ms)
{
if (ms is IConcurrentMergeScheduler concurrentMergeScheduler)
{
concurrentMergeScheduler.Sync();
}
}
/// <summary>
/// This runs the <see cref="Index.CheckIndex"/> tool on the index in. If any
/// issues are hit, an <see cref="Exception"/> is thrown; else,
/// true is returned.
/// </summary>
public static CheckIndex.Status CheckIndex(Directory dir)
{
return CheckIndex(dir, true);
}
public static CheckIndex.Status CheckIndex(Directory dir, bool crossCheckTermVectors)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
CheckIndex checker = new CheckIndex(dir);
checker.CrossCheckTermVectors = crossCheckTermVectors;
checker.InfoStream = new StreamWriter(bos, Encoding.UTF8);
CheckIndex.Status indexStatus = checker.DoCheckIndex(null);
if (indexStatus == null || indexStatus.Clean == false)
{
Console.WriteLine("CheckIndex failed");
checker.FlushInfoStream();
Console.WriteLine(bos.ToString());
throw new Exception("CheckIndex failed");
}
else
{
if (LuceneTestCase.UseInfoStream)
{
checker.FlushInfoStream();
Console.WriteLine(bos.ToString());
}
return indexStatus;
}
}
/// <summary>
/// This runs the <see cref="Index.CheckIndex"/> tool on the <see cref="IndexReader"/>. If any
/// issues are hit, an <see cref="Exception"/> is thrown.
/// </summary>
public static void CheckReader(IndexReader reader)
{
foreach (AtomicReaderContext context in reader.Leaves)
{
CheckReader(context.AtomicReader, true);
}
}
public static void CheckReader(AtomicReader reader, bool crossCheckTermVectors)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
StreamWriter infoStream = new StreamWriter(bos, Encoding.UTF8);
reader.CheckIntegrity();
CheckIndex.Status.FieldNormStatus fieldNormStatus = Index.CheckIndex.TestFieldNorms(reader, infoStream);
CheckIndex.Status.TermIndexStatus termIndexStatus = Index.CheckIndex.TestPostings(reader, infoStream);
CheckIndex.Status.StoredFieldStatus storedFieldStatus = Index.CheckIndex.TestStoredFields(reader, infoStream);
CheckIndex.Status.TermVectorStatus termVectorStatus = Index.CheckIndex.TestTermVectors(reader, infoStream, false, crossCheckTermVectors);
CheckIndex.Status.DocValuesStatus docValuesStatus = Index.CheckIndex.TestDocValues(reader, infoStream);
if (fieldNormStatus.Error != null || termIndexStatus.Error != null || storedFieldStatus.Error != null || termVectorStatus.Error != null || docValuesStatus.Error != null)
{
Console.WriteLine("CheckReader failed");
infoStream.Flush();
Console.WriteLine(bos.ToString());
throw new Exception("CheckReader failed");
}
else
{
if (LuceneTestCase.UseInfoStream)
{
Console.WriteLine(bos.ToString());
}
}
}
/// <summary>
/// <paramref name="start"/> and <paramref name="end"/> are BOTH inclusive </summary>
public static int NextInt32(Random r, int start, int end)
{
return RandomInts.RandomInt32Between(r, start, end);
}
/// <summary>
/// <paramref name="start"/> and <paramref name="end"/> are BOTH inclusive </summary>
public static long NextInt64(Random r, long start, long end)
{
Assert.True(end >= start);
BigInteger range = (BigInteger)end + (BigInteger)1 - (BigInteger)start;
if (range.CompareTo((BigInteger)int.MaxValue) <= 0)
{
return start + r.Next((int)range);
}
else
{
// probably not evenly distributed when range is large, but OK for tests
//BigInteger augend = BigInteger.Multiply(range, new BigInteger(r.NextDouble()));
//long result = start + (long)augend;
// LUCENENET NOTE: Using BigInteger/Decimal doesn't work because r.NextDouble() is always
// rounded down to 0, which makes the result always the same as start. This alternative solution was
// snagged from https://stackoverflow.com/a/13095144. All we really care about here is that we get
// a pretty good random distribution of values between start and end.
//Working with ulong so that modulo works correctly with values > long.MaxValue
ulong uRange = (ulong)unchecked(end - start);
//Prevent a modolo bias; see https://stackoverflow.com/a/10984975/238419
//for more information.
//In the worst case, the expected number of calls is 2 (though usually it's
//much closer to 1) so this loop doesn't really hurt performance at all.
ulong ulongRand;
do
{
byte[] buf = new byte[8];
r.NextBytes(buf);
ulongRand = (ulong)BitConverter.ToInt64(buf, 0);
} while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange);
long result = (long)(ulongRand % uRange) + start + r.Next(0, 1); // Randomly decide whether to increment by 1 to make the second parameter "inclusive"
Assert.True(result >= start);
Assert.True(result <= end);
return result;
}
}
/// <summary>
/// Returns a random string consisting only of lowercase characters 'a' through 'z'.
/// </summary>
public static string RandomSimpleString(Random r, int maxLength)
{
return RandomSimpleString(r, 0, maxLength);
}
/// <summary>
/// Returns a random string consisting only of lowercase characters 'a' through 'z'.
/// </summary>
public static string RandomSimpleString(Random r, int minLength, int maxLength)
{
int end = NextInt32(r, minLength, maxLength);
if (end == 0)
{
// allow 0 length
return "";
}
char[] buffer = new char[end];
for (int i = 0; i < end; i++)
{
buffer[i] = (char)TestUtil.NextInt32(r, 'a', 'z');
}
return new string(buffer, 0, end);
}
/// <summary>
/// Returns a random string consisting only of characters between <paramref name="minChar"/> and <paramref name="maxChar"/>.
/// </summary>
public static string RandomSimpleStringRange(Random r, char minChar, char maxChar, int maxLength)
{
int end = NextInt32(r, 0, maxLength);
if (end == 0)
{
// allow 0 length
return "";
}
char[] buffer = new char[end];
for (int i = 0; i < end; i++)
{
buffer[i] = (char)TestUtil.NextInt32(r, minChar, maxChar);
}
return new string(buffer, 0, end);
}
/// <summary>
/// Returns a random string consisting only of lowercase characters 'a' through 'z',
/// between 0 and 20 characters in length.
/// </summary>
public static string RandomSimpleString(Random r)
{
return RandomSimpleString(r, 0, 10);
}
/// <summary>
/// Returns random string, including full unicode range. </summary>
public static string RandomUnicodeString(Random r)
{
return RandomUnicodeString(r, 20);
}
/// <summary>
/// Returns a random string up to a certain length.
/// </summary>
public static string RandomUnicodeString(Random r, int maxLength)
{
int end = NextInt32(r, 0, maxLength);
if (end == 0)
{
// allow 0 length
return "";
}
char[] buffer = new char[end];
RandomFixedLengthUnicodeString(r, buffer, 0, buffer.Length);
return new string(buffer, 0, end);
}
/// <summary>
/// Fills provided <see cref="T:char[]"/> with valid random unicode code
/// unit sequence.
/// </summary>
public static void RandomFixedLengthUnicodeString(Random random, char[] chars, int offset, int length)
{
int i = offset;
int end = offset + length;
while (i < end)
{
int t = random.Next(5);
if (0 == t && i < length - 1)
{
// Make a surrogate pair
// High surrogate
chars[i++] = (char)NextInt32(random, 0xd800, 0xdbff);
// Low surrogate
chars[i++] = (char)NextInt32(random, 0xdc00, 0xdfff);
}
else if (t <= 1)
{
chars[i++] = (char)random.Next(0x80);
}
else if (2 == t)
{
chars[i++] = (char)NextInt32(random, 0x80, 0x7ff);
}
else if (3 == t)
{
chars[i++] = (char)NextInt32(random, 0x800, 0xd7ff);
}
else if (4 == t)
{
chars[i++] = (char)NextInt32(random, 0xe000, 0xffff);
}
}
}
/// <summary>
/// Returns a <see cref="string"/> thats "regexpish" (contains lots of operators typically found in regular expressions)
/// If you call this enough times, you might get a valid regex!
/// </summary>
public static string RandomRegexpishString(Random r)
{
return RandomRegexpishString(r, 20);
}
/// <summary>
/// Maximum recursion bound for '+' and '*' replacements in
/// <see cref="RandomRegexpishString(Random, int)"/>.
/// </summary>
private const int MaxRecursionBound = 5;
/// <summary>
/// Operators for <see cref="RandomRegexpishString(Random, int)"/>.
/// </summary>
private static readonly IList<string> ops = new string[] {
".", "?",
"{0," + MaxRecursionBound + "}", // bounded replacement for '*'
"{1," + MaxRecursionBound + "}", // bounded replacement for '+'
"(",
")",
"-",
"[",
"]",
"|"
};
/// <summary>
/// Returns a <see cref="string"/> thats "regexpish" (contains lots of operators typically found in regular expressions)
/// If you call this enough times, you might get a valid regex!
///
/// <para/>Note: to avoid practically endless backtracking patterns we replace asterisk and plus
/// operators with bounded repetitions. See LUCENE-4111 for more info.
/// </summary>
/// <param name="maxLength"> A hint about maximum length of the regexpish string. It may be exceeded by a few characters. </param>
public static string RandomRegexpishString(Random r, int maxLength)
{
StringBuilder regexp = new StringBuilder(maxLength);
for (int i = NextInt32(r, 0, maxLength); i > 0; i--)
{
if (r.NextBoolean())
{
regexp.Append((char)RandomInts.RandomInt32Between(r, 'a', 'z'));
}
else
{
regexp.Append(RandomPicks.RandomFrom(r, ops));
}
}
return regexp.ToString();
}
private static readonly string[] HTML_CHAR_ENTITIES = new string[]
{
"AElig", "Aacute", "Acirc", "Agrave", "Alpha", "AMP", "Aring", "Atilde", "Auml", "Beta", "COPY", "Ccedil", "Chi",
"Dagger", "Delta", "ETH", "Eacute", "Ecirc", "Egrave", "Epsilon", "Eta", "Euml", "Gamma", "GT", "Iacute", "Icirc",
"Igrave", "Iota", "Iuml", "Kappa", "Lambda", "LT", "Mu", "Ntilde", "Nu", "OElig", "Oacute", "Ocirc", "Ograve",
"Omega", "Omicron", "Oslash", "Otilde", "Ouml", "Phi", "Pi", "Prime", "Psi", "QUOT", "REG", "Rho", "Scaron",
"Sigma", "THORN", "Tau", "Theta", "Uacute", "Ucirc", "Ugrave", "Upsilon", "Uuml", "Xi", "Yacute", "Yuml", "Zeta",
"aacute", "acirc", "acute", "aelig", "agrave", "alefsym", "alpha", "amp", "and", "ang", "apos", "aring", "asymp",
"atilde", "auml", "bdquo", "beta", "brvbar", "bull", "cap", "ccedil", "cedil", "cent", "chi", "circ", "clubs",
"cong", "copy", "crarr", "cup", "curren", "dArr", "dagger", "darr", "deg", "delta", "diams", "divide", "eacute",
"ecirc", "egrave", "empty", "emsp", "ensp", "epsilon", "equiv", "eta", "eth", "euml", "euro", "exist", "fnof",
"forall", "frac12", "frac14", "frac34", "frasl", "gamma", "ge", "gt", "hArr", "harr", "hearts", "hellip", "iacute",
"icirc", "iexcl", "igrave", "image", "infin", "int", "iota", "iquest", "isin", "iuml", "kappa", "lArr", "lambda",
"lang", "laquo", "larr", "lceil", "ldquo", "le", "lfloor", "lowast", "loz", "lrm", "lsaquo", "lsquo", "lt", "macr",
"mdash", "micro", "middot", "minus", "mu", "nabla", "nbsp", "ndash", "ne", "ni", "not", "notin", "nsub", "ntilde",
"nu", "oacute", "ocirc", "oelig", "ograve", "oline", "omega", "omicron", "oplus", "or", "ordf", "ordm", "oslash",
"otilde", "otimes", "ouml", "para", "part", "permil", "perp", "phi", "pi", "piv", "plusmn", "pound", "prime", "prod",
"prop", "psi", "quot", "rArr", "radic", "rang", "raquo", "rarr", "rceil", "rdquo", "real", "reg", "rfloor", "rho",
"rlm", "rsaquo", "rsquo", "sbquo", "scaron", "sdot", "sect", "shy", "sigma", "sigmaf", "sim", "spades", "sub", "sube",
"sum", "sup", "sup1", "sup2", "sup3", "supe", "szlig", "tau", "there4", "theta", "thetasym", "thinsp", "thorn", "tilde",
"times", "trade", "uArr", "uacute", "uarr", "ucirc", "ugrave", "uml", "upsih", "upsilon", "uuml", "weierp", "xi",
"yacute", "yen", "yuml", "zeta", "zwj", "zwnj"
};
/// <summary>
/// Returns a random HTML-like string.
/// </summary>
public static string RandomHtmlishString(Random random, int numElements)
{
int end = NextInt32(random, 0, numElements);
if (end == 0)
{
// allow 0 length
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < end; i++)
{
int val = random.Next(25);
switch (val)
{
case 0:
sb.Append("<p>");
break;
case 1:
{
sb.Append("<");
sb.Append(" ".Substring(NextInt32(random, 0, 4)));
sb.Append(RandomSimpleString(random));
for (int j = 0; j < NextInt32(random, 0, 10); ++j)
{
sb.Append(' ');
sb.Append(RandomSimpleString(random));
sb.Append(" ".Substring(NextInt32(random, 0, 1)));
sb.Append('=');
sb.Append(" ".Substring(NextInt32(random, 0, 1)));
sb.Append("\"".Substring(NextInt32(random, 0, 1)));
sb.Append(RandomSimpleString(random));
sb.Append("\"".Substring(NextInt32(random, 0, 1)));
}
sb.Append(" ".Substring(NextInt32(random, 0, 4)));
sb.Append("/".Substring(NextInt32(random, 0, 1)));
sb.Append(">".Substring(NextInt32(random, 0, 1)));
break;
}
case 2:
{
sb.Append("</");
sb.Append(" ".Substring(NextInt32(random, 0, 4)));
sb.Append(RandomSimpleString(random));
sb.Append(" ".Substring(NextInt32(random, 0, 4)));
sb.Append(">".Substring(NextInt32(random, 0, 1)));
break;
}
case 3:
sb.Append(">");
break;
case 4:
sb.Append("</p>");
break;
case 5:
sb.Append("<!--");
break;
case 6:
sb.Append("<!--#");
break;
case 7:
sb.Append("<script><!-- f('");
break;
case 8:
sb.Append("</script>");
break;
case 9:
sb.Append("<?");
break;
case 10:
sb.Append("?>");
break;
case 11:
sb.Append("\"");
break;
case 12:
sb.Append("\\\"");
break;
case 13:
sb.Append("'");
break;
case 14:
sb.Append("\\'");
break;
case 15:
sb.Append("-->");
break;
case 16:
{
sb.Append("&");
switch (NextInt32(random, 0, 2))
{
case 0:
sb.Append(RandomSimpleString(random));
break;
case 1:
sb.Append(HTML_CHAR_ENTITIES[random.Next(HTML_CHAR_ENTITIES.Length)]);
break;
}
sb.Append(";".Substring(NextInt32(random, 0, 1)));
break;
}
case 17:
{
sb.Append("&#");
if (0 == NextInt32(random, 0, 1))
{
sb.Append(NextInt32(random, 0, int.MaxValue - 1));
sb.Append(";".Substring(NextInt32(random, 0, 1)));
}
break;
}
case 18:
{
sb.Append("&#x");
if (0 == NextInt32(random, 0, 1))
{
sb.Append(Convert.ToString(NextInt32(random, 0, int.MaxValue - 1), 16));
sb.Append(";".Substring(NextInt32(random, 0, 1)));
}
break;
}
case 19:
sb.Append(";");
break;
case 20:
sb.Append(NextInt32(random, 0, int.MaxValue - 1));
break;
case 21:
sb.Append("\n");
break;
case 22:
sb.Append(" ".Substring(NextInt32(random, 0, 10)));
break;
case 23:
{
sb.Append("<");
if (0 == NextInt32(random, 0, 3))
{
sb.Append(" ".Substring(NextInt32(random, 1, 10)));
}
if (0 == NextInt32(random, 0, 1))
{
sb.Append("/");
if (0 == NextInt32(random, 0, 3))
{
sb.Append(" ".Substring(NextInt32(random, 1, 10)));
}
}
switch (NextInt32(random, 0, 3))
{
case 0:
sb.Append(RandomlyRecaseString(random, "script"));
break;
case 1:
sb.Append(RandomlyRecaseString(random, "style"));
break;
case 2:
sb.Append(RandomlyRecaseString(random, "br"));
break;
// default: append nothing
}
sb.Append(">".Substring(NextInt32(random, 0, 1)));
break;
}
default:
sb.Append(RandomSimpleString(random));
break;
}
}
return sb.ToString();
}
/// <summary>
/// Randomly upcases, downcases, or leaves intact each code point in the given string.
/// </summary>
public static string RandomlyRecaseString(Random random, string str)
{
var builder = new StringBuilder();
int pos = 0;
while (pos < str.Length)
{
string toRecase;
// check if the next char sequence is a surrogate pair
if (pos + 1 < str.Length && char.IsSurrogatePair(str[pos], str[pos + 1]))
{
toRecase = str.Substring(pos, 2);
}
else
{
toRecase = str.Substring(pos, 1);
}
pos += toRecase.Length;
switch (NextInt32(random, 0, 2))
{
case 0:
builder.Append(toRecase.ToUpper()); // LUCENENET NOTE: Intentionally using current culture
break;
case 1:
builder.Append(toRecase.ToLower()); // LUCENENET NOTE: Intentionally using current culture
break;
case 2:
builder.Append(toRecase);
break;
}
}
return builder.ToString();
}
private static readonly int[] blockStarts = new int[]
{
0x0000, 0x0080, 0x0100, 0x0180, 0x0250, 0x02B0, 0x0300, 0x0370, 0x0400, 0x0500, 0x0530, 0x0590, 0x0600, 0x0700,
0x0750, 0x0780, 0x07C0, 0x0800, 0x0900, 0x0980, 0x0A00, 0x0A80, 0x0B00, 0x0B80, 0x0C00, 0x0C80, 0x0D00, 0x0D80,
0x0E00, 0x0E80, 0x0F00, 0x1000, 0x10A0, 0x1100, 0x1200, 0x1380, 0x13A0, 0x1400, 0x1680, 0x16A0, 0x1700, 0x1720,
0x1740, 0x1760, 0x1780, 0x1800, 0x18B0, 0x1900, 0x1950, 0x1980, 0x19E0, 0x1A00, 0x1A20, 0x1B00, 0x1B80, 0x1C00,
0x1C50, 0x1CD0, 0x1D00, 0x1D80, 0x1DC0, 0x1E00, 0x1F00, 0x2000, 0x2070, 0x20A0, 0x20D0, 0x2100, 0x2150, 0x2190,
0x2200, 0x2300, 0x2400, 0x2440, 0x2460, 0x2500, 0x2580, 0x25A0, 0x2600, 0x2700, 0x27C0, 0x27F0, 0x2800, 0x2900,
0x2980, 0x2A00, 0x2B00, 0x2C00, 0x2C60, 0x2C80, 0x2D00, 0x2D30, 0x2D80, 0x2DE0, 0x2E00, 0x2E80, 0x2F00, 0x2FF0,
0x3000, 0x3040, 0x30A0, 0x3100, 0x3130, 0x3190, 0x31A0, 0x31C0, 0x31F0, 0x3200, 0x3300, 0x3400, 0x4DC0, 0x4E00,
0xA000, 0xA490, 0xA4D0, 0xA500, 0xA640, 0xA6A0, 0xA700, 0xA720, 0xA800, 0xA830, 0xA840, 0xA880, 0xA8E0, 0xA900,
0xA930, 0xA960, 0xA980, 0xAA00, 0xAA60, 0xAA80, 0xABC0, 0xAC00, 0xD7B0, 0xE000, 0xF900, 0xFB00, 0xFB50, 0xFE00,
0xFE10, 0xFE20, 0xFE30, 0xFE50, 0xFE70, 0xFF00, 0xFFF0, 0x10000, 0x10080, 0x10100, 0x10140, 0x10190, 0x101D0,
0x10280, 0x102A0, 0x10300, 0x10330, 0x10380, 0x103A0, 0x10400, 0x10450, 0x10480, 0x10800, 0x10840, 0x10900,
0x10920, 0x10A00, 0x10A60, 0x10B00, 0x10B40, 0x10B60, 0x10C00, 0x10E60, 0x11080, 0x12000, 0x12400, 0x13000,
0x1D000, 0x1D100, 0x1D200, 0x1D300, 0x1D360, 0x1D400, 0x1F000, 0x1F030, 0x1F100, 0x1F200, 0x20000, 0x2A700,
0x2F800, 0xE0000, 0xE0100, 0xF0000, 0x100000
};
private static readonly int[] blockEnds = new int[]
{
0x007F, 0x00FF, 0x017F, 0x024F, 0x02AF, 0x02FF, 0x036F, 0x03FF, 0x04FF, 0x052F, 0x058F, 0x05FF, 0x06FF, 0x074F,
0x077F, 0x07BF, 0x07FF, 0x083F, 0x097F, 0x09FF, 0x0A7F, 0x0AFF, 0x0B7F, 0x0BFF, 0x0C7F, 0x0CFF, 0x0D7F, 0x0DFF,
0x0E7F, 0x0EFF, 0x0FFF, 0x109F, 0x10FF, 0x11FF, 0x137F, 0x139F, 0x13FF, 0x167F, 0x169F, 0x16FF, 0x171F, 0x173F,
0x175F, 0x177F, 0x17FF, 0x18AF, 0x18FF, 0x194F, 0x197F, 0x19DF, 0x19FF, 0x1A1F, 0x1AAF, 0x1B7F, 0x1BBF, 0x1C4F,
0x1C7F, 0x1CFF, 0x1D7F, 0x1DBF, 0x1DFF, 0x1EFF, 0x1FFF, 0x206F, 0x209F, 0x20CF, 0x20FF, 0x214F, 0x218F, 0x21FF,
0x22FF, 0x23FF, 0x243F, 0x245F, 0x24FF, 0x257F, 0x259F, 0x25FF, 0x26FF, 0x27BF, 0x27EF, 0x27FF, 0x28FF, 0x297F,
0x29FF, 0x2AFF, 0x2BFF, 0x2C5F, 0x2C7F, 0x2CFF, 0x2D2F, 0x2D7F, 0x2DDF, 0x2DFF, 0x2E7F, 0x2EFF, 0x2FDF, 0x2FFF,
0x303F, 0x309F, 0x30FF, 0x312F, 0x318F, 0x319F, 0x31BF, 0x31EF, 0x31FF, 0x32FF, 0x33FF, 0x4DBF, 0x4DFF, 0x9FFF,
0xA48F, 0xA4CF, 0xA4FF, 0xA63F, 0xA69F, 0xA6FF, 0xA71F, 0xA7FF, 0xA82F, 0xA83F, 0xA87F, 0xA8DF, 0xA8FF, 0xA92F,
0xA95F, 0xA97F, 0xA9DF, 0xAA5F, 0xAA7F, 0xAADF, 0xABFF, 0xD7AF, 0xD7FF, 0xF8FF, 0xFAFF, 0xFB4F, 0xFDFF, 0xFE0F,
0xFE1F, 0xFE2F, 0xFE4F, 0xFE6F, 0xFEFF, 0xFFEF, 0xFFFF, 0x1007F, 0x100FF, 0x1013F, 0x1018F, 0x101CF, 0x101FF,
0x1029F, 0x102DF, 0x1032F, 0x1034F, 0x1039F, 0x103DF, 0x1044F, 0x1047F, 0x104AF, 0x1083F, 0x1085F, 0x1091F,
0x1093F, 0x10A5F, 0x10A7F, 0x10B3F, 0x10B5F, 0x10B7F, 0x10C4F, 0x10E7F, 0x110CF, 0x123FF, 0x1247F, 0x1342F,
0x1D0FF, 0x1D1FF, 0x1D24F, 0x1D35F, 0x1D37F, 0x1D7FF, 0x1F02F, 0x1F09F, 0x1F1FF, 0x1F2FF, 0x2A6DF, 0x2B73F,
0x2FA1F, 0xE007F, 0xE01EF, 0xFFFFF, 0x10FFFF
};
/// <summary>
/// Returns random string of length between 0-20 codepoints, all codepoints within the same unicode block. </summary>
public static string RandomRealisticUnicodeString(Random r)
{
return RandomRealisticUnicodeString(r, 20);
}
/// <summary>
/// Returns random string of length up to maxLength codepoints, all codepoints within the same unicode block. </summary>
public static string RandomRealisticUnicodeString(Random r, int maxLength)
{
return RandomRealisticUnicodeString(r, 0, maxLength);
}
/// <summary>
/// Returns random string of length between min and max codepoints, all codepoints within the same unicode block. </summary>
public static string RandomRealisticUnicodeString(Random r, int minLength, int maxLength)
{
int end = NextInt32(r, minLength, maxLength);
int block = r.Next(blockStarts.Length);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < end; i++)
{
sb.AppendCodePoint(NextInt32(r, blockStarts[block], blockEnds[block]));
}
return sb.ToString();
}
/// <summary>
/// Returns random string, with a given UTF-8 byte <paramref name="length"/>. </summary>
public static string RandomFixedByteLengthUnicodeString(Random r, int length)
{
char[] buffer = new char[length * 3];
int bytes = length;
int i = 0;
for (; i < buffer.Length && bytes != 0; i++)
{
int t;
if (bytes >= 4)
{
t = r.Next(5);
}
else if (bytes >= 3)
{
t = r.Next(4);
}
else if (bytes >= 2)
{
t = r.Next(2);
}
else
{
t = 0;
}
if (t == 0)
{
buffer[i] = (char)r.Next(0x80);
bytes--;
}
else if (1 == t)
{
buffer[i] = (char)NextInt32(r, 0x80, 0x7ff);
bytes -= 2;
}
else if (2 == t)
{
buffer[i] = (char)NextInt32(r, 0x800, 0xd7ff);
bytes -= 3;
}
else if (3 == t)
{
buffer[i] = (char)NextInt32(r, 0xe000, 0xffff);
bytes -= 3;
}
else if (4 == t)
{
// Make a surrogate pair
// High surrogate
buffer[i++] = (char)NextInt32(r, 0xd800, 0xdbff);
// Low surrogate
buffer[i] = (char)NextInt32(r, 0xdc00, 0xdfff);
bytes -= 4;
}
}
return new string(buffer, 0, i);
}
/// <summary>
/// Return a <see cref="Codec"/> that can read any of the
/// default codecs and formats, but always writes in the specified
/// format.
/// </summary>
public static Codec AlwaysPostingsFormat(PostingsFormat format)
{
// TODO: we really need for postings impls etc to announce themselves
// (and maybe their params, too) to infostream on flush and merge.
// otherwise in a real debugging situation we won't know whats going on!
if (LuceneTestCase.Verbose)
{
Console.WriteLine("forcing postings format to:" + format);
}
return new Lucene46CodecAnonymousClass(format);
}
private class Lucene46CodecAnonymousClass : Lucene46Codec
{
private readonly PostingsFormat format;
public Lucene46CodecAnonymousClass(PostingsFormat format)
{
this.format = format;
}
public override PostingsFormat GetPostingsFormatForField(string field)
{
return format;
}
}
/// <summary>
/// Return a <see cref="Codec"/> that can read any of the
/// default codecs and formats, but always writes in the specified
/// format.
/// </summary>
public static Codec AlwaysDocValuesFormat(DocValuesFormat format)
{
// TODO: we really need for docvalues impls etc to announce themselves
// (and maybe their params, too) to infostream on flush and merge.
// otherwise in a real debugging situation we won't know whats going on!
if (LuceneTestCase.Verbose)
{
Console.WriteLine("forcing docvalues format to:" + format);
}
return new Lucene46CodecAnonymousClass2(format);
}
private class Lucene46CodecAnonymousClass2 : Lucene46Codec
{
private readonly DocValuesFormat format;
public Lucene46CodecAnonymousClass2(DocValuesFormat format)
{
this.format = format;
}
public override DocValuesFormat GetDocValuesFormatForField(string field)
{
return format;
}
}
// TODO: generalize all 'test-checks-for-crazy-codecs' to
// annotations (LUCENE-3489)
public static string GetPostingsFormat(string field)
{
return GetPostingsFormat(Codec.Default, field);
}
public static string GetPostingsFormat(Codec codec, string field)
{
PostingsFormat p = codec.PostingsFormat;
if (p is PerFieldPostingsFormat perFieldPostingsFormat)
{
return perFieldPostingsFormat.GetPostingsFormatForField(field).Name;
}
else
{
return p.Name;
}
}
public static string GetDocValuesFormat(string field)
{
return GetDocValuesFormat(Codec.Default, field);
}
public static string GetDocValuesFormat(Codec codec, string field)
{
DocValuesFormat f = codec.DocValuesFormat;
if (f is PerFieldDocValuesFormat perFieldDocValuesFormat)
{
return perFieldDocValuesFormat.GetDocValuesFormatForField(field).Name;
}
else
{
return f.Name;
}
}
// TODO: remove this, push this test to Lucene40/Lucene42 codec tests
public static bool FieldSupportsHugeBinaryDocValues(string field)
{
string dvFormat = GetDocValuesFormat(field);
if (dvFormat.Equals("Lucene40", StringComparison.Ordinal)
|| dvFormat.Equals("Lucene42", StringComparison.Ordinal)
|| dvFormat.Equals("Memory", StringComparison.Ordinal))
{
return false;
}
return true;
}
public static bool AnyFilesExceptWriteLock(Directory dir)
{
string[] files = dir.ListAll();
if (files.Length > 1 || (files.Length == 1 && !files[0].Equals("write.lock", StringComparison.Ordinal)))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Just tries to configure things to keep the open file
/// count lowish.
/// </summary>
public static void ReduceOpenFiles(IndexWriter w)
{
// keep number of open files lowish
MergePolicy mp = w.Config.MergePolicy;
if (mp is LogMergePolicy lmp)
{
lmp.MergeFactor = Math.Min(5, lmp.MergeFactor);
lmp.NoCFSRatio = 1.0;
}
else if (mp is TieredMergePolicy tmp)
{
tmp.MaxMergeAtOnce = Math.Min(5, tmp.MaxMergeAtOnce);
tmp.SegmentsPerTier = Math.Min(5, tmp.SegmentsPerTier);
tmp.NoCFSRatio = 1.0;
}
IMergeScheduler ms = w.Config.MergeScheduler;
if (ms is IConcurrentMergeScheduler concurrentMergeScheduler)
{
// wtf... shouldnt it be even lower since its 1 by default?!?!
concurrentMergeScheduler.SetMaxMergesAndThreads(3, 2);
}
}