Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of Replacement for Debug.Assert() that can be turned on/off in Release build (addresses #326) #327

Merged
merged 13 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
29 changes: 17 additions & 12 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ name: 'vNext$(rev:.r)' # Format for build number (will be overridden)
# Testing variables

# RunTests: 'true' (Optional - set to 'false' to disable test jobs - useful for debugging. If not provided, tests will be run.)
# AssertsEnabled: 'true' (Optional - set to 'false' to run tests without asserts, which is less thorough. This can speed up testing and verify the application will run without asserts.)
# IsNightly: 'false' (Optional - set to 'true' to run additional tests for the nightly build)
# IsWeekly: 'false' (Optional - set to 'true' to run additional tests for the weekly build)
# RunSlowTests: 'true' (Optional - set to 'false' to skip slow tests to make testing time shorter)
Expand Down Expand Up @@ -143,6 +144,7 @@ stages:
- pwsh: |
# Generate a lucene.testsettings.json file for use with the test framework
$assert = if ($Env:AssertsEnabled -ne 'false') { 'true' } else { 'false' }
$nightly = if ($Env:IsNightly -eq 'true') { 'true' } else { 'false' }
$weekly = if ($Env:IsWeekly -eq 'true') { 'true' } else { 'false' }
$slow = if ($Env:RunSlowTests -ne 'false') { 'true' } else { 'false' }
Expand All @@ -153,18 +155,21 @@ stages:
$directory = if ($Env:Directory -eq $null) { 'random' } else { $Env:Directory }
$verbose = if ($Env:Verbose -eq 'true') { 'true' } else { 'false' }
$multiplier = if ($Env:Multiplier -eq $null) { '1' } else { $Env:Multiplier }
$fileText = "{`n`t""tests"":`n`t{`n`t`t" +
"""nightly"": ""$nightly"",`n`t`t" +
"""weekly"": ""$weekly"",`n`t`t" +
"""slow"": ""$slow"",`n`t`t" +
"""awaitsfix"": ""$awaitsFix"",`n`t`t" +
"""codec"": ""$codec"",`n`t`t" +
"""docvaluesformat"": ""$docValuesFormat"",`n`t`t" +
"""postingsformat"": ""$postingsFormat"",`n`t`t" +
"""directory"": ""$directory"",`n`t`t" +
"""verbose"": ""$verbose"",`n`t`t" +
"""multiplier"": ""$multiplier""`n`t" +
"}`n}"
$fileText = "{`n`t" +
"""assert"": ""$assert"",`n`t" +
"""tests"": {`n`t`t" +
"""nightly"": ""$nightly"",`n`t`t" +
"""weekly"": ""$weekly"",`n`t`t" +
"""slow"": ""$slow"",`n`t`t" +
"""awaitsfix"": ""$awaitsFix"",`n`t`t" +
"""codec"": ""$codec"",`n`t`t" +
"""docvaluesformat"": ""$docValuesFormat"",`n`t`t" +
"""postingsformat"": ""$postingsFormat"",`n`t`t" +
"""directory"": ""$directory"",`n`t`t" +
"""verbose"": ""$verbose"",`n`t`t" +
"""multiplier"": ""$multiplier""`n`t" +
"}`n" +
"}"
Out-File -filePath "$(Build.ArtifactStagingDirectory)/$(TestSettingsFileName)" -encoding UTF8 -inputObject $fileText
displayName: 'Persist Test Settings to lucene.testsettings.json'
condition: and(succeeded(), ne(variables['RunTests'], 'false'))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Support;
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using Lucene.Net.Util;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -113,7 +114,8 @@ protected virtual void AddOffCorrectMap(int off, int cumulativeDiff)
}

int offset = offsets[(size == 0) ? 0 : size - 1];
Debug.Assert(size == 0 || off >= offset, "Offset #" + size + "(" + off + ") is less than the last recorded offset " + offset + "\n" + Arrays.ToString(offsets) + "\n" + Arrays.ToString(diffs));
if (Debugging.AssertsEnabled) Debugging.Assert(size == 0 || off >= offset,
() => "Offset #" + size + "(" + off + ") is less than the last recorded offset " + offset + "\n" + Arrays.ToString(offsets) + "\n" + Arrays.ToString(diffs));

if (size == 0 || off != offsets[size - 1])
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using J2N;
using Lucene.Net.Analysis.Util;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -30952,7 +30953,7 @@ internal void Restart()
/// </summary>
internal int NextChar()
{
Debug.Assert(!IsRead, "Attempting to read past the end of a segment.");
if (Debugging.AssertsEnabled) Debugging.Assert(!IsRead, () => "Attempting to read past the end of a segment.");
return m_buf[pos++];
}

Expand Down Expand Up @@ -31377,7 +31378,7 @@ private int NextChar()
}
catch (Exception /*e*/)
{
Debug.Assert(false, "Exception parsing code point '" + decimalCharRef + "'");
if (Debugging.AssertsEnabled) Debugging.Assert(false, () => "Exception parsing code point '" + decimalCharRef + "'");
}
if (codePoint <= 0x10FFFF)
{
Expand Down Expand Up @@ -31637,7 +31638,7 @@ string hexCharRef
}
catch (Exception /*e*/)
{
Debug.Assert(false, "Exception parsing hex code point '" + hexCharRef + "'");
if (Debugging.AssertsEnabled) Debugging.Assert(false, () => "Exception parsing hex code point '" + hexCharRef + "'");
}
if (codePoint <= 0x10FFFF)
{
Expand Down Expand Up @@ -31900,7 +31901,7 @@ string hexCharRef
}
catch (Exception /*e*/)
{ // should never happen
Debug.Assert(false, "Exception parsing high surrogate '"
if (Debugging.AssertsEnabled) Debugging.Assert(false, () => "Exception parsing high surrogate '"
+ surrogatePair.Substring(2, 6 - 2) + "'");
}
try
Expand All @@ -31909,7 +31910,7 @@ string hexCharRef
}
catch (Exception /*e*/)
{ // should never happen
Debug.Assert(false, "Exception parsing low surrogate '" + surrogatePair.Substring(10, 14 - 10) + "'");
if (Debugging.AssertsEnabled) Debugging.Assert(false, () => "Exception parsing low surrogate '" + surrogatePair.Substring(10, 14 - 10) + "'");
}
// add (previously matched input length) + (this match length) - (substitution length)
cumulativeDiff += inputSegment.Length + YyLength - 2;
Expand All @@ -31931,7 +31932,7 @@ string hexCharRef
}
catch (Exception /*e*/)
{ // should never happen
Debug.Assert(false, "Exception parsing high surrogate '"
if (Debugging.AssertsEnabled) Debugging.Assert(false, () => "Exception parsing high surrogate '"
+ surrogatePair.Substring(2, 6 - 2) + "'");
}
try
Expand All @@ -31940,7 +31941,7 @@ string hexCharRef
}
catch (Exception /*e*/)
{ // should never happen
Debug.Assert(false, "Exception parsing low surrogate '"
if (Debugging.AssertsEnabled) Debugging.Assert(false, () => "Exception parsing low surrogate '"
+ surrogatePair.Substring(9, 14 - 9) + "'");
}
if (char.IsLowSurrogate(lowSurrogate))
Expand Down Expand Up @@ -31972,7 +31973,7 @@ string hexCharRef
}
catch (Exception /*e*/)
{ // should never happen
Debug.Assert(false, "Exception parsing high surrogate '"
if (Debugging.AssertsEnabled) Debugging.Assert(false, () => "Exception parsing high surrogate '"
+ surrogatePair.Substring(1, 6 - 1) + "'");
}
if (char.IsHighSurrogate(highSurrogate))
Expand All @@ -31985,7 +31986,7 @@ string hexCharRef
}
catch (Exception /*e*/)
{ // should never happen
Debug.Assert(false, "Exception parsing low surrogate '"
if (Debugging.AssertsEnabled) Debugging.Assert(false, () => "Exception parsing low surrogate '"
+ surrogatePair.Substring(10, 14 - 10) + "'");
}
// add (previously matched input length) + (this match length) - (substitution length)
Expand All @@ -32012,7 +32013,7 @@ string hexCharRef
}
catch (Exception /*e*/)
{ // should never happen
Debug.Assert(false, "Exception parsing high surrogate '"
if (Debugging.AssertsEnabled) Debugging.Assert(false, () => "Exception parsing high surrogate '"
+ surrogatePair.Substring(1, 6 - 1) + "'");
}
if (char.IsHighSurrogate(highSurrogate))
Expand All @@ -32024,7 +32025,7 @@ string hexCharRef
}
catch (Exception /*e*/)
{ // should never happen
Debug.Assert(false, "Exception parsing low surrogate '"
if (Debugging.AssertsEnabled) Debugging.Assert(false, () => "Exception parsing low surrogate '"
+ surrogatePair.Substring(9, 14 - 9) + "'");
}
if (char.IsLowSurrogate(lowSurrogate))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Analysis.Util;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using Lucene.Net.Util.Fst;
using System;
Expand Down Expand Up @@ -135,7 +136,7 @@ public override int Read()
if (!FST.TargetHasArcs(arc))
{
// Fast pass for single character match:
Debug.Assert(arc.IsFinal);
if (Debugging.AssertsEnabled) Debugging.Assert(arc.IsFinal);
lastMatchLen = 1;
lastMatch = arc.Output;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Lucene.Net.Util;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using Lucene.Net.Util.Fst;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using JCG = J2N.Collections.Generic;

Expand Down Expand Up @@ -54,7 +54,7 @@ private NormalizeCharMap(FST<CharsRef> map)
map.ReadFirstRealTargetArc(scratchArc.Target, scratchArc, fstReader);
while (true)
{
Debug.Assert(scratchArc.Label != FST.END_LABEL);
if (Debugging.AssertsEnabled) Debugging.Assert(scratchArc.Label != FST.END_LABEL);
cachedRootArcs[Convert.ToChar((char)scratchArc.Label)] = (new FST.Arc<CharsRef>()).CopyFrom(scratchArc);
if (scratchArc.IsLast)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using J2N.Text;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Analysis.Util;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Lucene.Net.Analysis.Compound
{
Expand Down Expand Up @@ -110,7 +110,7 @@ public override sealed bool IncrementToken()
{
if (m_tokens.Count > 0)
{
Debug.Assert(current != null);
if (Debugging.AssertsEnabled) Debugging.Assert(current != null);
CompoundToken token = m_tokens.Dequeue();
RestoreState(current); // keep all other attributes untouched
m_termAtt.SetEmpty().Append(token.Text);
Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net.Analysis.Common/Analysis/Gl/GalicianStemmer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Diagnostics;
using Lucene.Net.Analysis.Pt;
using Lucene.Net.Diagnostics;
using System.Collections.Generic;
using Lucene.Net.Analysis.Pt;

namespace Lucene.Net.Analysis.Gl
{
Expand Down Expand Up @@ -47,7 +47,7 @@ static GalicianStemmer()
/// <returns> new valid length, stemmed </returns>
public virtual int Stem(char[] s, int len)
{
Debug.Assert(s.Length >= len + 1, "this stemmer requires an oversized array of at least 1");
if (Debugging.AssertsEnabled) Debugging.Assert(s.Length >= len + 1, () => "this stemmer requires an oversized array of at least 1");

len = plural.Apply(s, len);
len = unification.Apply(s, len);
Expand Down
10 changes: 5 additions & 5 deletions src/Lucene.Net.Analysis.Common/Analysis/Hunspell/Dictionary.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using J2N.Collections.Generic.Extensions;
using J2N;
using J2N.Collections.Generic.Extensions;
using J2N.Text;
using Lucene.Net.Diagnostics;
using Lucene.Net.Store;
using Lucene.Net.Support;
using Lucene.Net.Support.IO;
using Lucene.Net.Util;
using Lucene.Net.Util.Automaton;
using Lucene.Net.Util.Fst;
using J2N;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -375,7 +375,7 @@ private void ReadAffixFile(Stream affixStream, Encoding decoder)
strip.CopyTo(0, stripData, currentOffset, strip.Length - 0);
currentOffset += strip.Length;
}
Debug.Assert(currentIndex == seenStrips.Count);
if (Debugging.AssertsEnabled) Debugging.Assert(currentIndex == seenStrips.Count);
stripOffsets[currentIndex] = currentOffset;
}

Expand Down Expand Up @@ -424,7 +424,7 @@ private void ParseAffix(JCG.SortedDictionary<string, IList<char?>> affixes, stri

for (int i = 0; i < numLines; i++)
{
Debug.Assert(affixWriter.Position == currentAffix << 3);
if (Debugging.AssertsEnabled) Debugging.Assert(affixWriter.Position == currentAffix << 3);
string line = reader.ReadLine();
string[] ruleArgs = whitespacePattern.Split(line).TrimEnd();

Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net.Analysis.Common/Analysis/Hunspell/Stemmer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Lucene.Net.Analysis.Util;
using Lucene.Net.Diagnostics;
using Lucene.Net.Store;
using Lucene.Net.Util;
using Lucene.Net.Util.Automaton;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

Expand Down Expand Up @@ -210,7 +210,7 @@ private IList<CharsRef> Stem(char[] word, int length, int previous, int prevFlag
// cross check incoming continuation class (flag of previous affix) against list.
dictionary.flagLookup.Get(append, scratch);
char[] appendFlags = Dictionary.DecodeFlags(scratch);
Debug.Assert(prevFlag >= 0);
if (Debugging.AssertsEnabled) Debugging.Assert(prevFlag >= 0);
compatible = HasCrossCheckedFlag((char)prevFlag, appendFlags, false);
}
else
Expand Down Expand Up @@ -279,7 +279,7 @@ private IList<CharsRef> Stem(char[] word, int length, int previous, int prevFlag
// cross check incoming continuation class (flag of previous affix) against list.
dictionary.flagLookup.Get(append, scratch);
char[] appendFlags = Dictionary.DecodeFlags(scratch);
Debug.Assert(prevFlag >= 0);
if (Debugging.AssertsEnabled) Debugging.Assert(prevFlag >= 0);
compatible = HasCrossCheckedFlag((char)prevFlag, appendFlags, previousWasPrefix);
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Diagnostics;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;

namespace Lucene.Net.Analysis.Miscellaneous
Expand Down Expand Up @@ -92,7 +92,7 @@ public override bool IncrementToken()
{
if (state != null)
{
Debug.Assert(preserveOriginal, "state should only be captured if preserveOriginal is true");
if (Debugging.AssertsEnabled) Debugging.Assert(preserveOriginal, () => "state should only be captured if preserveOriginal is true");
RestoreState(state);
posIncAttr.PositionIncrement = 0;
state = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Diagnostics;
using System.Diagnostics;

namespace Lucene.Net.Analysis.Miscellaneous
Expand Down Expand Up @@ -34,11 +35,11 @@ public sealed class SingleTokenTokenStream : TokenStream
public SingleTokenTokenStream(Token token)
: base(Token.TOKEN_ATTRIBUTE_FACTORY)
{
Debug.Assert(token != null);
if (Debugging.AssertsEnabled) Debugging.Assert(token != null);
this.singleToken = (Token)token.Clone();

tokenAtt = AddAttribute<ICharTermAttribute>();
Debug.Assert(tokenAtt is Token);
if (Debugging.AssertsEnabled) Debugging.Assert(tokenAtt is Token);
}

public override sealed bool IncrementToken()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using J2N;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Analysis.Util;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using System;
using System.Diagnostics;
Expand Down Expand Up @@ -231,7 +232,7 @@ public override sealed bool IncrementToken()
{
if (bufferStart + 1 + minGram > bufferEnd)
{
Debug.Assert(exhausted);
if (Debugging.AssertsEnabled) Debugging.Assert(exhausted);
return false;
}
Consume();
Expand Down Expand Up @@ -294,7 +295,7 @@ protected virtual bool IsTokenChar(int chr)
public override sealed void End()
{
base.End();
Debug.Assert(bufferStart <= bufferEnd);
if (Debugging.AssertsEnabled) Debugging.Assert(bufferStart <= bufferEnd);
int endOffset = offset;
for (int i = bufferStart; i < bufferEnd; ++i)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using System.Diagnostics;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -154,7 +155,7 @@ public override bool IncrementToken()
{
if (currentMatcher != -1 && NextCapture())
{
Debug.Assert(state != null);
if (Debugging.AssertsEnabled) Debugging.Assert(state != null);
ClearAttributes();
RestoreState(state);
int start = matchers[currentMatcher].Groups[currentGroup[currentMatcher]].Index;
Expand Down
Loading