-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathTestPackedInts.cs
More file actions
1530 lines (1420 loc) · 64.6 KB
/
Copy pathTestPackedInts.cs
File metadata and controls
1530 lines (1420 loc) · 64.6 KB
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.IO;
using Lucene.Net.Support;
using NUnit.Framework;
using RandomizedTesting.Generators;
using System;
using System.Collections.Generic;
using System.Globalization;
using JCG = J2N.Collections.Generic;
using RandomInts = RandomizedTesting.Generators.RandomNumbers;
namespace Lucene.Net.Util.Packed
{
/*
* 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.
*/
using Assert = Lucene.Net.TestFramework.Assert;
using ByteArrayDataInput = Lucene.Net.Store.ByteArrayDataInput;
using CodecUtil = Lucene.Net.Codecs.CodecUtil;
using DataInput = Lucene.Net.Store.DataInput;
using Directory = Lucene.Net.Store.Directory;
using IndexInput = Lucene.Net.Store.IndexInput;
using IndexOutput = Lucene.Net.Store.IndexOutput;
using IOContext = Lucene.Net.Store.IOContext;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;
using Reader = Lucene.Net.Util.Packed.PackedInt32s.Reader;
[TestFixture]
public class TestPackedInts : LuceneTestCase
{
[Test]
public virtual void TestByteCount()
{
int iters = AtLeast(3);
for (int i = 0; i < iters; ++i)
{
int valueCount = RandomInts.RandomInt32Between(Random, 1, int.MaxValue);
foreach (PackedInt32s.Format format in PackedInt32s.Format.Values)
{
for (int bpv = 1; bpv <= 64; ++bpv)
{
long byteCount = format.ByteCount(PackedInt32s.VERSION_CURRENT, valueCount, bpv);
string msg = "format=" + format + ", byteCount=" + byteCount + ", valueCount=" + valueCount + ", bpv=" + bpv;
Assert.IsTrue(byteCount * 8 >= (long)valueCount * bpv, msg);
if (format == PackedInt32s.Format.PACKED)
{
Assert.IsTrue((byteCount - 1) * 8 < (long)valueCount * bpv, msg);
}
}
}
}
}
[Test]
public virtual void TestBitsRequired()
{
Assert.AreEqual(61, PackedInt32s.BitsRequired((long)Math.Pow(2, 61) - 1));
Assert.AreEqual(61, PackedInt32s.BitsRequired(0x1FFFFFFFFFFFFFFFL));
Assert.AreEqual(62, PackedInt32s.BitsRequired(0x3FFFFFFFFFFFFFFFL));
Assert.AreEqual(63, PackedInt32s.BitsRequired(0x7FFFFFFFFFFFFFFFL));
}
[Test]
public virtual void TestMaxValues()
{
Assert.AreEqual(1, PackedInt32s.MaxValue(1), "1 bit -> max == 1");
Assert.AreEqual(3, PackedInt32s.MaxValue(2), "2 bit -> max == 3");
Assert.AreEqual(255, PackedInt32s.MaxValue(8), "8 bit -> max == 255");
Assert.AreEqual(long.MaxValue, PackedInt32s.MaxValue(63), "63 bit -> max == Long.MAX_VALUE");
Assert.AreEqual(long.MaxValue, PackedInt32s.MaxValue(64), "64 bit -> max == Long.MAX_VALUE (same as for 63 bit)");
}
[Test]
public virtual void TestPackedInts_Mem()
{
int num = AtLeast(3);
for (int iter = 0; iter < num; iter++)
{
for (int nbits = 1; nbits <= 64; nbits++)
{
long maxValue = PackedInt32s.MaxValue(nbits);
int valueCount = TestUtil.NextInt32(Random, 1, 600);
int bufferSize = Random.NextBoolean() ? TestUtil.NextInt32(Random, 0, 48) : TestUtil.NextInt32(Random, 0, 4096);
Directory d = NewDirectory();
IndexOutput @out = d.CreateOutput("out.bin", NewIOContext(Random));
float acceptableOverhead;
if (iter == 0)
{
// have the first iteration go through exact nbits
acceptableOverhead = 0.0f;
}
else
{
acceptableOverhead = Random.NextSingle();
}
PackedInt32s.Writer w = PackedInt32s.GetWriter(@out, valueCount, nbits, acceptableOverhead);
long startFp = @out.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream
int actualValueCount = Random.NextBoolean() ? valueCount : TestUtil.NextInt32(Random, 0, valueCount);
long[] values = new long[valueCount];
for (int i = 0; i < actualValueCount; i++)
{
if (nbits == 64)
{
values[i] = Random.NextInt64();
}
else
{
values[i] = TestUtil.NextInt64(Random, 0, maxValue);
}
w.Add(values[i]);
}
w.Finish();
long fp = @out.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream
@out.Dispose();
// ensure that finish() added the (valueCount-actualValueCount) missing values
long bytes = w.Format.ByteCount(PackedInt32s.VERSION_CURRENT, valueCount, w.BitsPerValue);
Assert.AreEqual(bytes, fp - startFp);
{ // test header
IndexInput @in = d.OpenInput("out.bin", NewIOContext(Random));
// header = codec header | bitsPerValue | valueCount | format
CodecUtil.CheckHeader(@in, PackedInt32s.CODEC_NAME, PackedInt32s.VERSION_START, PackedInt32s.VERSION_CURRENT); // codec header
Assert.AreEqual(w.BitsPerValue, @in.ReadVInt32());
Assert.AreEqual(valueCount, @in.ReadVInt32());
Assert.AreEqual(w.Format.Id, @in.ReadVInt32());
Assert.AreEqual(startFp, @in.Position); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
@in.Dispose();
}
{ // test reader
IndexInput @in = d.OpenInput("out.bin", NewIOContext(Random));
PackedInt32s.Reader r = PackedInt32s.GetReader(@in);
Assert.AreEqual(fp, @in.Position); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
for (int i = 0; i < valueCount; i++)
{
Assert.AreEqual(values[i], r.Get(i), "index=" + i + " valueCount=" + valueCount + " nbits=" + nbits + " for " + r.GetType().Name);
}
@in.Dispose();
long expectedBytesUsed = RamUsageEstimator.SizeOf(r);
long computedBytesUsed = r.RamBytesUsed();
Assert.AreEqual(expectedBytesUsed, computedBytesUsed, r.GetType() + "expected " + expectedBytesUsed + ", got: " + computedBytesUsed);
}
{ // test reader iterator next
IndexInput @in = d.OpenInput("out.bin", NewIOContext(Random));
PackedInt32s.IReaderIterator r = PackedInt32s.GetReaderIterator(@in, bufferSize);
for (int i = 0; i < valueCount; i++)
{
Assert.AreEqual(values[i], r.Next(), "index=" + i + " valueCount=" + valueCount + " nbits=" + nbits + " for " + r.GetType().Name);
Assert.AreEqual(i, r.Ord);
}
assertEquals(fp, @in.Position); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
@in.Dispose();
}
{ // test reader iterator bulk next
IndexInput @in = d.OpenInput("out.bin", NewIOContext(Random));
PackedInt32s.IReaderIterator r = PackedInt32s.GetReaderIterator(@in, bufferSize);
int i = 0;
while (i < valueCount)
{
int count = TestUtil.NextInt32(Random, 1, 95);
Int64sRef next = r.Next(count);
for (int k = 0; k < next.Length; ++k)
{
Assert.AreEqual(values[i + k], next.Int64s[next.Offset + k], "index=" + i + " valueCount=" + valueCount + " nbits=" + nbits + " for " + r.GetType().Name);
}
i += next.Length;
}
Assert.AreEqual(fp, @in.Position); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
@in.Dispose();
}
{ // test direct reader get
IndexInput @in = d.OpenInput("out.bin", NewIOContext(Random));
PackedInt32s.Reader intsEnum = PackedInt32s.GetDirectReader(@in);
for (int i = 0; i < valueCount; i++)
{
string msg = "index=" + i + " valueCount=" + valueCount + " nbits=" + nbits + " for " + intsEnum.GetType().Name;
int index = Random.Next(valueCount);
Assert.AreEqual(values[index], intsEnum.Get(index), msg);
}
intsEnum.Get(intsEnum.Count - 1);
Assert.AreEqual(fp, @in.Position); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
@in.Dispose();
}
d.Dispose();
}
}
}
[Test]
public virtual void TestEndPointer()
{
Directory dir = NewDirectory();
int valueCount = RandomInts.RandomInt32Between(Random, 1, 1000);
IndexOutput @out = dir.CreateOutput("tests.bin", NewIOContext(Random));
for (int i = 0; i < valueCount; ++i)
{
@out.WriteInt64(0);
}
@out.Dispose();
IndexInput @in = dir.OpenInput("tests.bin", NewIOContext(Random));
for (int version = PackedInt32s.VERSION_START; version <= PackedInt32s.VERSION_CURRENT; ++version)
{
for (int bpv = 1; bpv <= 64; ++bpv)
{
foreach (PackedInt32s.Format format in PackedInt32s.Format.Values)
{
if (!format.IsSupported(bpv))
{
continue;
}
long byteCount = format.ByteCount(version, valueCount, bpv);
string msg = "format=" + format + ",version=" + version + ",valueCount=" + valueCount + ",bpv=" + bpv;
// test iterator
@in.Seek(0L);
PackedInt32s.IReaderIterator it = PackedInt32s.GetReaderIteratorNoHeader(@in, format, version, valueCount, bpv, RandomInts.RandomInt32Between(Random, 1, 1 << 16));
for (int i = 0; i < valueCount; ++i)
{
it.Next();
}
Assert.AreEqual(byteCount, @in.Position, msg); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
// test direct reader
@in.Seek(0L);
PackedInt32s.Reader directReader = PackedInt32s.GetDirectReaderNoHeader(@in, format, version, valueCount, bpv);
directReader.Get(valueCount - 1);
Assert.AreEqual(byteCount, @in.Position, msg); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
// test reader
@in.Seek(0L);
PackedInt32s.GetReaderNoHeader(@in, format, version, valueCount, bpv);
Assert.AreEqual(byteCount, @in.Position, msg); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
}
}
}
@in.Dispose();
dir.Dispose();
}
[Test]
public virtual void TestControlledEquality()
{
const int VALUE_COUNT = 255;
const int BITS_PER_VALUE = 8;
IList<PackedInt32s.Mutable> packedInts = CreatePackedInts(VALUE_COUNT, BITS_PER_VALUE);
foreach (PackedInt32s.Mutable packedInt in packedInts)
{
for (int i = 0; i < packedInt.Count; i++)
{
packedInt.Set(i, i + 1);
}
}
AssertListEquality(packedInts);
}
[Test]
public virtual void TestRandomBulkCopy()
{
int numIters = AtLeast(3);
for (int iter = 0; iter < numIters; iter++)
{
if (Verbose)
{
Console.WriteLine("\nTEST: iter=" + iter);
}
int valueCount = AtLeast(100000);
int bits1 = TestUtil.NextInt32(Random, 1, 64);
int bits2 = TestUtil.NextInt32(Random, 1, 64);
if (bits1 > bits2)
{
int tmp = bits1;
bits1 = bits2;
bits2 = tmp;
}
if (Verbose)
{
Console.WriteLine(" valueCount=" + valueCount + " bits1=" + bits1 + " bits2=" + bits2);
}
PackedInt32s.Mutable packed1 = PackedInt32s.GetMutable(valueCount, bits1, PackedInt32s.COMPACT);
PackedInt32s.Mutable packed2 = PackedInt32s.GetMutable(valueCount, bits2, PackedInt32s.COMPACT);
long maxValue = PackedInt32s.MaxValue(bits1);
for (int i = 0; i < valueCount; i++)
{
long val = TestUtil.NextInt64(Random, 0, maxValue);
packed1.Set(i, val);
packed2.Set(i, val);
}
long[] buffer = new long[valueCount];
// Copy random slice over, 20 times:
for (int iter2 = 0; iter2 < 20; iter2++)
{
int start = Random.Next(valueCount - 1);
int len = TestUtil.NextInt32(Random, 1, valueCount - start);
int offset;
if (Verbose)
{
Console.WriteLine(" copy " + len + " values @ " + start);
}
if (len == valueCount)
{
offset = 0;
}
else
{
offset = Random.Next(valueCount - len);
}
if (Random.NextBoolean())
{
int got = packed1.Get(start, buffer, offset, len);
Assert.IsTrue(got <= len);
int sot = packed2.Set(start, buffer, offset, got);
Assert.IsTrue(sot <= got);
}
else
{
PackedInt32s.Copy(packed1, offset, packed2, offset, len, Random.Next(10 * len));
}
/*
for(int i=0;i<valueCount;i++) {
Assert.AreEqual("value " + i, packed1.Get(i), packed2.Get(i));
}
*/
}
for (int i = 0; i < valueCount; i++)
{
Assert.AreEqual(packed1.Get(i), packed2.Get(i), "value " + i);
}
}
}
[Test]
public virtual void TestRandomEquality()
{
int numIters = AtLeast(2);
for (int i = 0; i < numIters; ++i)
{
int valueCount = TestUtil.NextInt32(Random, 1, 300);
for (int bitsPerValue = 1; bitsPerValue <= 64; bitsPerValue++)
{
AssertRandomEquality(valueCount, bitsPerValue, Random.NextInt64());
}
}
}
private static void AssertRandomEquality(int valueCount, int bitsPerValue, long randomSeed)
{
IList<PackedInt32s.Mutable> packedInts = CreatePackedInts(valueCount, bitsPerValue);
foreach (PackedInt32s.Mutable packedInt in packedInts)
{
try
{
Fill(packedInt, PackedInt32s.MaxValue(bitsPerValue), randomSeed);
}
catch (Exception e) when (e.IsException())
{
e.PrintStackTrace(Console.Error);
Assert.Fail(string.Format(CultureInfo.InvariantCulture, "Exception while filling {0}: valueCount={1}, bitsPerValue={2}", packedInt.GetType().Name, valueCount, bitsPerValue));
}
}
AssertListEquality(packedInts);
}
private static IList<PackedInt32s.Mutable> CreatePackedInts(int valueCount, int bitsPerValue)
{
IList<PackedInt32s.Mutable> packedInts = new JCG.List<PackedInt32s.Mutable>();
if (bitsPerValue <= 8)
{
packedInts.Add(new Direct8(valueCount));
}
if (bitsPerValue <= 16)
{
packedInts.Add(new Direct16(valueCount));
}
if (bitsPerValue <= 24 && valueCount <= Packed8ThreeBlocks.MAX_SIZE)
{
packedInts.Add(new Packed8ThreeBlocks(valueCount));
}
if (bitsPerValue <= 32)
{
packedInts.Add(new Direct32(valueCount));
}
if (bitsPerValue <= 48 && valueCount <= Packed16ThreeBlocks.MAX_SIZE)
{
packedInts.Add(new Packed16ThreeBlocks(valueCount));
}
if (bitsPerValue <= 63)
{
packedInts.Add(new Packed64(valueCount, bitsPerValue));
}
packedInts.Add(new Direct64(valueCount));
for (int bpv = bitsPerValue; bpv <= Packed64SingleBlock.MAX_SUPPORTED_BITS_PER_VALUE; ++bpv)
{
if (Packed64SingleBlock.IsSupported(bpv))
{
packedInts.Add(Packed64SingleBlock.Create(valueCount, bpv));
}
}
return packedInts;
}
private static void Fill(PackedInt32s.Mutable packedInt, long maxValue, long randomSeed)
{
Random rnd2 = new J2N.Randomizer(randomSeed);
for (int i = 0; i < packedInt.Count; i++)
{
long value = TestUtil.NextInt64(rnd2, 0, maxValue);
packedInt.Set(i, value);
Assert.AreEqual(value, packedInt.Get(i), string.Format(CultureInfo.InvariantCulture, "The set/get of the value at index {0} should match for {1}", i, packedInt.GetType().Name));
}
}
private static void AssertListEquality<T1>(IList<T1> packedInts) where T1 : PackedInt32s.Reader
{
AssertListEquality("", packedInts);
}
private static void AssertListEquality<T1>(string message, IList<T1> packedInts) where T1 : PackedInt32s.Reader
{
if (packedInts.Count == 0)
{
return;
}
PackedInt32s.Reader @base = packedInts[0];
int valueCount = @base.Count;
foreach (PackedInt32s.Reader packedInt in packedInts)
{
Assert.AreEqual(valueCount, packedInt.Count, message + ". The number of values should be the same ");
}
for (int i = 0; i < valueCount; i++)
{
for (int j = 1; j < packedInts.Count; j++)
{
Assert.AreEqual(@base.Get(i), packedInts[j].Get(i), string.Format(CultureInfo.InvariantCulture, "{0}. The value at index {1} should be the same for {2} and {3}", message, i, @base.GetType().Name, packedInts[j].GetType().Name));
}
}
}
[Test]
public virtual void TestSingleValue()
{
for (int bitsPerValue = 1; bitsPerValue <= 64; ++bitsPerValue)
{
Directory dir = NewDirectory();
IndexOutput @out = dir.CreateOutput("out", NewIOContext(Random));
PackedInt32s.Writer w = PackedInt32s.GetWriter(@out, 1, bitsPerValue, PackedInt32s.DEFAULT);
long value = 17L & PackedInt32s.MaxValue(bitsPerValue);
w.Add(value);
w.Finish();
long end = @out.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream
@out.Dispose();
IndexInput @in = dir.OpenInput("out", NewIOContext(Random));
Reader reader = PackedInt32s.GetReader(@in);
string msg = "Impl=" + w.GetType().Name + ", bitsPerValue=" + bitsPerValue;
Assert.AreEqual(1, reader.Count, msg);
Assert.AreEqual(value, reader.Get(0), msg);
Assert.AreEqual(end, @in.Position, msg); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
@in.Dispose();
dir.Dispose();
}
}
[Test]
public virtual void TestSecondaryBlockChange()
{
PackedInt32s.Mutable mutable = new Packed64(26, 5);
mutable.Set(24, 31);
Assert.AreEqual(31, mutable.Get(24), "The value #24 should be correct");
mutable.Set(4, 16);
Assert.AreEqual(31, mutable.Get(24), "The value #24 should remain unchanged");
}
/*
Check if the structures properly handle the case where
index * bitsPerValue > Integer.MAX_VALUE
NOTE: this test allocates 256 MB
*/
[Ignore("See LUCENE-4488 - LUCENENET NOTE: In .NET it is not possible to catch OOME")]
[Test]
public virtual void TestIntOverflow()
{
int INDEX = (int)Math.Pow(2, 30) + 1;
int BITS = 2;
Packed64 p64 = null;
try
{
p64 = new Packed64(INDEX, BITS);
}
catch (Exception oome) when (oome.IsOutOfMemoryError())
{
// this can easily happen: we're allocating a
// long[] that needs 256-273 MB. Heap is 512 MB,
// but not all of that is available for large
// objects ... empirical testing shows we only
// have ~ 67 MB free.
}
if (p64 != null)
{
p64.Set(INDEX - 1, 1);
Assert.AreEqual(1, p64.Get(INDEX - 1), "The value at position " + (INDEX - 1) + " should be correct for Packed64");
p64 = null;
}
Packed64SingleBlock p64sb = null;
try
{
p64sb = Packed64SingleBlock.Create(INDEX, BITS);
}
catch (Exception oome) when (oome.IsOutOfMemoryError())
{
// Ignore: see comment above
}
if (p64sb != null)
{
p64sb.Set(INDEX - 1, 1);
Assert.AreEqual(1, p64sb.Get(INDEX - 1), "The value at position " + (INDEX - 1) + " should be correct for " + p64sb.GetType().Name);
}
int index = int.MaxValue / 24 + 1;
Packed8ThreeBlocks p8 = null;
try
{
p8 = new Packed8ThreeBlocks(index);
}
catch (Exception oome) when (oome.IsOutOfMemoryError())
{
// Ignore: see comment above
}
if (p8 != null)
{
p8.Set(index - 1, 1);
Assert.AreEqual(1, p8.Get(index - 1), "The value at position " + (index - 1) + " should be correct for Packed8ThreeBlocks");
p8 = null;
}
index = int.MaxValue / 48 + 1;
Packed16ThreeBlocks p16 = null;
try
{
p16 = new Packed16ThreeBlocks(index);
}
catch (Exception oome) when (oome.IsOutOfMemoryError())
{
// Ignore: see comment above
}
if (p16 != null)
{
p16.Set(index - 1, 1);
Assert.AreEqual(1, p16.Get(index - 1), "The value at position " + (index - 1) + " should be correct for Packed16ThreeBlocks");
p16 = null;
}
}
[Test]
public virtual void TestFill()
{
const int valueCount = 1111;
int from = Random.Next(valueCount + 1);
int to = from + Random.Next(valueCount + 1 - from);
for (int bpv = 1; bpv <= 64; ++bpv)
{
long val = TestUtil.NextInt64(Random, 0, PackedInt32s.MaxValue(bpv));
IList<PackedInt32s.Mutable> packedInts = CreatePackedInts(valueCount, bpv);
foreach (PackedInt32s.Mutable ints in packedInts)
{
string msg = ints.GetType().Name + " bpv=" + bpv + ", from=" + from + ", to=" + to + ", val=" + val;
ints.Fill(0, ints.Count, 1);
ints.Fill(from, to, val);
for (int i = 0; i < ints.Count; ++i)
{
if (i >= from && i < to)
{
Assert.AreEqual(val, ints.Get(i), msg + ", i=" + i);
}
else
{
Assert.AreEqual(1, ints.Get(i), msg + ", i=" + i);
}
}
}
}
}
[Test]
public virtual void TestPackedIntsNull()
{
// must be > 10 for the bulk reads below
int size = TestUtil.NextInt32(Random, 11, 256);
Reader packedInts = new PackedInt32s.NullReader(size);
Assert.AreEqual(0, packedInts.Get(TestUtil.NextInt32(Random, 0, size - 1)));
long[] arr = new long[size + 10];
int r;
Arrays.Fill(arr, 1);
r = packedInts.Get(0, arr, 0, size - 1);
Assert.AreEqual(size - 1, r);
for (r--; r >= 0; r--)
{
Assert.AreEqual(0, arr[r]);
}
Arrays.Fill(arr, 1);
r = packedInts.Get(10, arr, 0, size + 10);
Assert.AreEqual(size - 10, r);
for (int i = 0; i < size - 10; i++)
{
Assert.AreEqual(0, arr[i]);
}
}
[Test]
public virtual void TestBulkGet()
{
const int valueCount = 1111;
int index = Random.Next(valueCount);
int len = TestUtil.NextInt32(Random, 1, valueCount * 2);
int off = Random.Next(77);
for (int bpv = 1; bpv <= 64; ++bpv)
{
long mask = PackedInt32s.MaxValue(bpv);
IList<PackedInt32s.Mutable> packedInts = CreatePackedInts(valueCount, bpv);
foreach (PackedInt32s.Mutable ints in packedInts)
{
for (int i = 0; i < ints.Count; ++i)
{
ints.Set(i, (31L * i - 1099) & mask);
}
long[] arr = new long[off + len];
string msg = ints.GetType().Name + " valueCount=" + valueCount + ", index=" + index + ", len=" + len + ", off=" + off;
int gets = ints.Get(index, arr, off, len);
Assert.IsTrue(gets > 0, msg);
Assert.IsTrue(gets <= len, msg);
Assert.IsTrue(gets <= ints.Count - index, msg);
for (int i = 0; i < arr.Length; ++i)
{
string m = msg + ", i=" + i;
if (i >= off && i < off + gets)
{
Assert.AreEqual(ints.Get(i - off + index), arr[i], m);
}
else
{
Assert.AreEqual(0, arr[i], m);
}
}
}
}
}
[Test]
public virtual void TestBulkSet()
{
const int valueCount = 1111;
int index = Random.Next(valueCount);
int len = TestUtil.NextInt32(Random, 1, valueCount * 2);
int off = Random.Next(77);
long[] arr = new long[off + len];
for (int bpv = 1; bpv <= 64; ++bpv)
{
long mask = PackedInt32s.MaxValue(bpv);
IList<PackedInt32s.Mutable> packedInts = CreatePackedInts(valueCount, bpv);
for (int i = 0; i < arr.Length; ++i)
{
arr[i] = (31L * i + 19) & mask;
}
foreach (PackedInt32s.Mutable ints in packedInts)
{
string msg = ints.GetType().Name + " valueCount=" + valueCount + ", index=" + index + ", len=" + len + ", off=" + off;
int sets = ints.Set(index, arr, off, len);
Assert.IsTrue(sets > 0, msg);
Assert.IsTrue(sets <= len, msg);
for (int i = 0; i < ints.Count; ++i)
{
string m = msg + ", i=" + i;
if (i >= index && i < index + sets)
{
Assert.AreEqual(arr[off - index + i], ints.Get(i), m);
}
else
{
Assert.AreEqual(0, ints.Get(i), m);
}
}
}
}
}
[Test]
public virtual void TestCopy()
{
int valueCount = TestUtil.NextInt32(Random, 5, 600);
int off1 = Random.Next(valueCount);
int off2 = Random.Next(valueCount);
int len = Random.Next(Math.Min(valueCount - off1, valueCount - off2));
int mem = Random.Next(1024);
for (int bpv = 1; bpv <= 64; ++bpv)
{
long mask = PackedInt32s.MaxValue(bpv);
foreach (PackedInt32s.Mutable r1 in CreatePackedInts(valueCount, bpv))
{
for (int i = 0; i < r1.Count; ++i)
{
r1.Set(i, (31L * i - 1023) & mask);
}
foreach (PackedInt32s.Mutable r2 in CreatePackedInts(valueCount, bpv))
{
string msg = "src=" + r1 + ", dest=" + r2 + ", srcPos=" + off1 + ", destPos=" + off2 + ", len=" + len + ", mem=" + mem;
PackedInt32s.Copy(r1, off1, r2, off2, len, mem);
for (int i = 0; i < r2.Count; ++i)
{
string m = msg + ", i=" + i;
if (i >= off2 && i < off2 + len)
{
Assert.AreEqual(r1.Get(i - off2 + off1), r2.Get(i), m);
}
else
{
Assert.AreEqual(0, r2.Get(i), m);
}
}
}
}
}
}
[Test]
public virtual void TestGrowableWriter()
{
int valueCount = 113 + Random.Next(1111);
GrowableWriter wrt = new GrowableWriter(1, valueCount, PackedInt32s.DEFAULT);
wrt.Set(4, 2);
wrt.Set(7, 10);
wrt.Set(valueCount - 10, 99);
wrt.Set(99, 999);
wrt.Set(valueCount - 1, 1 << 10);
Assert.AreEqual(1 << 10, wrt.Get(valueCount - 1));
wrt.Set(99, (1 << 23) - 1);
Assert.AreEqual(1 << 10, wrt.Get(valueCount - 1));
wrt.Set(1, long.MaxValue);
wrt.Set(2, -3);
Assert.AreEqual(64, wrt.BitsPerValue);
Assert.AreEqual(1 << 10, wrt.Get(valueCount - 1));
Assert.AreEqual(long.MaxValue, wrt.Get(1));
Assert.AreEqual(-3L, wrt.Get(2));
Assert.AreEqual(2, wrt.Get(4));
Assert.AreEqual((1 << 23) - 1, wrt.Get(99));
Assert.AreEqual(10, wrt.Get(7));
Assert.AreEqual(99, wrt.Get(valueCount - 10));
Assert.AreEqual(1 << 10, wrt.Get(valueCount - 1));
Assert.AreEqual(RamUsageEstimator.SizeOf(wrt), wrt.RamBytesUsed());
}
[Test]
public virtual void TestPagedGrowableWriter()
{
int pageSize = 1 << (TestUtil.NextInt32(Random, 6, 30));
// supports 0 values?
PagedGrowableWriter writer = new PagedGrowableWriter(0, pageSize, TestUtil.NextInt32(Random, 1, 64), Random.NextSingle());
Assert.AreEqual(0, writer.Count);
// compare against AppendingDeltaPackedLongBuffer
AppendingDeltaPackedInt64Buffer buf = new AppendingDeltaPackedInt64Buffer();
int size = Random.Next(1000000);
long max = 5;
for (int i = 0; i < size; ++i)
{
buf.Add(TestUtil.NextInt64(Random, 0, max));
if (Rarely())
{
max = PackedInt32s.MaxValue(Rarely() ? TestUtil.NextInt32(Random, 0, 63) : TestUtil.NextInt32(Random, 0, 31));
}
}
writer = new PagedGrowableWriter(size, pageSize, TestUtil.NextInt32(Random, 1, 64), Random.NextSingle());
Assert.AreEqual(size, writer.Count);
for (int i = size - 1; i >= 0; --i)
{
writer.Set(i, buf.Get(i));
}
for (int i = 0; i < size; ++i)
{
Assert.AreEqual(buf.Get(i), writer.Get(i));
}
// test ramBytesUsed
Assert.AreEqual(RamUsageEstimator.SizeOf(writer), writer.RamBytesUsed(), 8);
// test copy
PagedGrowableWriter copy = writer.Resize(TestUtil.NextInt64(Random, writer.Count / 2, writer.Count * 3 / 2));
for (long i = 0; i < copy.Count; ++i)
{
if (i < writer.Count)
{
Assert.AreEqual(writer.Get(i), copy.Get(i));
}
else
{
Assert.AreEqual(0, copy.Get(i));
}
}
// test grow
PagedGrowableWriter grow = writer.Grow(TestUtil.NextInt64(Random, writer.Count / 2, writer.Count * 3 / 2));
for (long i = 0; i < grow.Count; ++i)
{
if (i < writer.Count)
{
Assert.AreEqual(writer.Get(i), grow.Get(i));
}
else
{
Assert.AreEqual(0, grow.Get(i));
}
}
}
[Test]
public virtual void TestPagedMutable()
{
int bitsPerValue = TestUtil.NextInt32(Random, 1, 64);
long max = PackedInt32s.MaxValue(bitsPerValue);
int pageSize = 1 << (TestUtil.NextInt32(Random, 6, 30));
// supports 0 values?
PagedMutable writer = new PagedMutable(0, pageSize, bitsPerValue, Random.NextSingle() / 2);
Assert.AreEqual(0, writer.Count);
// compare against AppendingDeltaPackedLongBuffer
AppendingDeltaPackedInt64Buffer buf = new AppendingDeltaPackedInt64Buffer();
int size = Random.Next(1000000);
for (int i = 0; i < size; ++i)
{
buf.Add(bitsPerValue == 64 ? Random.NextInt64() : TestUtil.NextInt64(Random, 0, max));
}
writer = new PagedMutable(size, pageSize, bitsPerValue, Random.NextSingle());
Assert.AreEqual(size, writer.Count);
for (int i = size - 1; i >= 0; --i)
{
writer.Set(i, buf.Get(i));
}
for (int i = 0; i < size; ++i)
{
Assert.AreEqual(buf.Get(i), writer.Get(i));
}
// test ramBytesUsed
Assert.AreEqual(RamUsageEstimator.SizeOf(writer) - RamUsageEstimator.SizeOf(writer.format), writer.RamBytesUsed());
// test copy
PagedMutable copy = writer.Resize(TestUtil.NextInt64(Random, writer.Count / 2, writer.Count * 3 / 2));
for (long i = 0; i < copy.Count; ++i)
{
if (i < writer.Count)
{
Assert.AreEqual(writer.Get(i), copy.Get(i));
}
else
{
Assert.AreEqual(0, copy.Get(i));
}
}
// test grow
PagedMutable grow = writer.Grow(TestUtil.NextInt64(Random, writer.Count / 2, writer.Count * 3 / 2));
for (long i = 0; i < grow.Count; ++i)
{
if (i < writer.Count)
{
Assert.AreEqual(writer.Get(i), grow.Get(i));
}
else
{
Assert.AreEqual(0, grow.Get(i));
}
}
}
[Ignore("// memory hole")]
[Test]
public virtual void TestPagedGrowableWriterOverflow()
{
long size = TestUtil.NextInt64(Random, 2 * (long)int.MaxValue, 3 * (long)int.MaxValue);
int pageSize = 1 << (TestUtil.NextInt32(Random, 16, 30));
PagedGrowableWriter writer = new PagedGrowableWriter(size, pageSize, 1, Random.NextSingle());
long index = TestUtil.NextInt64(Random, (long)int.MaxValue, size - 1);
writer.Set(index, 2);
Assert.AreEqual(2, writer.Get(index));
for (int i = 0; i < 1000000; ++i)
{
long idx = TestUtil.NextInt64(Random, 0, size);
if (idx == index)
{
Assert.AreEqual(2, writer.Get(idx));
}
else
{
Assert.AreEqual(0, writer.Get(idx));
}
}
}
[Test]
public virtual void TestSave()
{
int valueCount = TestUtil.NextInt32(Random, 1, 2048);
for (int bpv = 1; bpv <= 64; ++bpv)
{
int maxValue = (int)Math.Min(PackedInt32s.MaxValue(31), PackedInt32s.MaxValue(bpv));
RAMDirectory directory = new RAMDirectory();
IList<PackedInt32s.Mutable> packedInts = CreatePackedInts(valueCount, bpv);
foreach (PackedInt32s.Mutable mutable in packedInts)
{
for (int i = 0; i < mutable.Count; ++i)
{
mutable.Set(i, Random.Next(maxValue));
}
IndexOutput @out = directory.CreateOutput("packed-ints.bin", IOContext.DEFAULT);
mutable.Save(@out);
@out.Dispose();
IndexInput @in = directory.OpenInput("packed-ints.bin", IOContext.DEFAULT);
PackedInt32s.Reader reader = PackedInt32s.GetReader(@in);
Assert.AreEqual(mutable.BitsPerValue, reader.BitsPerValue);
Assert.AreEqual(valueCount, reader.Count);
if (mutable is Packed64SingleBlock)
{
// make sure that we used the right format so that the reader has
// the same performance characteristics as the mutable that has been
// serialized
Assert.IsTrue(reader is Packed64SingleBlock);
}
else
{
Assert.IsFalse(reader is Packed64SingleBlock);
}
for (int i = 0; i < valueCount; ++i)
{
Assert.AreEqual(mutable.Get(i), reader.Get(i));
}
@in.Dispose();
directory.DeleteFile("packed-ints.bin");
}
directory.Dispose();
}
}
[Test]
public virtual void TestEncodeDecode()
{
foreach (PackedInt32s.Format format in PackedInt32s.Format.Values)
{
for (int bpv = 1; bpv <= 64; ++bpv)
{
if (!format.IsSupported(bpv))
{
continue;
}
string msg = format + " " + bpv;
PackedInt32s.IEncoder encoder = PackedInt32s.GetEncoder(format, PackedInt32s.VERSION_CURRENT, bpv);
PackedInt32s.IDecoder decoder = PackedInt32s.GetDecoder(format, PackedInt32s.VERSION_CURRENT, bpv);
int longBlockCount = encoder.Int64BlockCount;