private void Check(IBytesRefSorter sorter) { for (int i = 0; i < 100; i++) { byte[] current = new byte[Random.nextInt(256)]; Random.NextBytes(current); sorter.Add(new BytesRef(current)); } // Create two iterators and check that they're aligned with each other. IBytesRefIterator i1 = sorter.GetIterator(); IBytesRefIterator i2 = sorter.GetIterator(); // Verify sorter contract. try { sorter.Add(new BytesRef(new byte[1])); fail("expected contract violation."); } catch (InvalidOperationException /*e*/) { // Expected. } BytesRef spare1; BytesRef spare2; while ((spare1 = i1.Next()) != null && (spare2 = i2.Next()) != null) { assertEquals(spare1, spare2); } assertNull(i1.Next()); assertNull(i2.Next()); }
private void Check(IBytesRefSorter sorter) { for (int i = 0; i < 100; i++) { byte[] current = new byte[Random.Next(256)]; Random.NextBytes(current); sorter.Add(new BytesRef(current)); } // Create two iterators and check that they're aligned with each other. IBytesRefEnumerator i1 = sorter.GetEnumerator(); IBytesRefEnumerator i2 = sorter.GetEnumerator(); // Verify sorter contract. try { sorter.Add(new BytesRef(new byte[1])); fail("expected contract violation."); } catch (Exception e) when(e.IsIllegalStateException()) { // Expected. } while (i1.MoveNext() && i2.MoveNext()) { assertEquals(i1.Current, i2.Current); } assertFalse(i1.MoveNext()); assertFalse(i2.MoveNext()); }
/// <summary> /// Appends a single suggestion and its weight to the internal buffers. /// </summary> /// <param name="utf8"> /// The suggestion (utf8 representation) to be added. The content is /// copied and the object can be reused. </param> /// <param name="bucket"> /// The bucket to place this suggestion in. Must be non-negative and /// smaller than the number of buckets passed in the constructor. /// Higher numbers indicate suggestions that should be presented /// before suggestions placed in smaller buckets. </param> public virtual void Add(BytesRef utf8, int bucket) { if (bucket < 0 || bucket >= buckets) { throw new ArgumentException("Bucket outside of the allowed range [0, " + buckets + "): " + bucket); } if (scratch.Bytes.Length < utf8.Length + 1) { scratch.Grow(utf8.Length + 10); } scratch.Length = 1; scratch.Bytes[0] = (byte)bucket; scratch.Append(utf8); sorter.Add(scratch); }
/// <summary> /// Appends a single suggestion and its weight to the internal buffers. /// </summary> /// <param name="utf8"> /// The suggestion (utf8 representation) to be added. The content is /// copied and the object can be reused. </param> /// <param name="bucket"> /// The bucket to place this suggestion in. Must be non-negative and /// smaller than the number of buckets passed in the constructor. /// Higher numbers indicate suggestions that should be presented /// before suggestions placed in smaller buckets. </param> public virtual void Add(BytesRef utf8, int bucket) { // LUCENENET: Added guard clause for null if (utf8 is null) { throw new ArgumentNullException(nameof(utf8)); } if (bucket < 0 || bucket >= buckets) { throw new ArgumentOutOfRangeException(nameof(buckets), "Bucket outside of the allowed range [0, " + buckets + "): " + bucket); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) } if (scratch.Bytes.Length < utf8.Length + 1) { scratch.Grow(utf8.Length + 10); } scratch.Length = 1; scratch.Bytes[0] = (byte)bucket; scratch.Append(utf8); sorter.Add(scratch); }