Skip to content

Commit

Permalink
Fixes for edge case "000"
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrondeau committed Apr 25, 2017
1 parent e8cfa72 commit 3e4b641
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/LZString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ private static string Decompress(int length, int resetValue, Func<int, char> get

for (i = 0; i < 3; i += 1)
{
dictionary.Add(i.ToString());
dictionary.Add(((char)i).ToString());
}

maxpower = (int)Math.Pow(2, 2);
Expand Down Expand Up @@ -537,7 +537,7 @@ private static string Decompress(int length, int resetValue, Func<int, char> get
}
else
{
if (c == dictionary.Count)
if (c2 == dictionary.Count)
{
entry = w + w[0];
}
Expand Down
35 changes: 28 additions & 7 deletions tests/LZStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public static IEnumerable<LZStringTestCase> TestCases()
CompressedUTF16 = "䴁䧆ಹ僨ᾦ≬ᢠ",
CompressedEncodedURIComponent = "mcMmmGTNDIPwyJMMQ"
};

yield return new LZStringTestCase
{
Name = "Edge Case 1",
Uncompressed = "000",
Compressed = \0",
CompressedBase64 = "Awo=",
CompressedUTF16 = "ƥ ",
CompressedEncodedURIComponent = "Awo"
};
}

[TestCaseSource(nameof(TestCases))]
Expand Down Expand Up @@ -74,7 +84,6 @@ public void CompressToEncodedURIComponent(LZStringTestCase test)
Assert.That(LZString.CompressToEncodedURIComponent(test.Uncompressed), Is.EqualTo(test.CompressedEncodedURIComponent));
}


[TestCaseSource(nameof(TestCases))]
public void DecompressFromEncodedURIComponent(LZStringTestCase test)
{
Expand All @@ -94,15 +103,19 @@ public void CompatibilityDecompressEncodedURIComponentFromNode(LZStringTestCase
{
Assert.Fail($"Invalid EncodedURIComponent string: '{compress}'{Environment.NewLine}{exc.Message}");
}
Assert.That(uncompress, Is.EqualTo(test.Uncompressed), $"Compression result: {compress}");
Console.WriteLine("lz-string compression result:");
Console.WriteLine(compress);
Assert.That(uncompress, Is.EqualTo(test.Uncompressed));
}

[TestCaseSource(nameof(TestCases))]
public void CompatibilityCompressEncodedURIComponentFromCSharp(LZStringTestCase test)
{
var compress = LZString.CompressToEncodedURIComponent(test.Uncompressed);
var uncompress = RunNodeLzString("decompressFromEncodedURIComponent", compress);
Assert.That(uncompress, Is.EqualTo(test.Uncompressed), $"Compression result: {compress}");
Console.WriteLine("c# compression result:");
Console.WriteLine(compress);
Assert.That(uncompress, Is.EqualTo(test.Uncompressed));
}

[TestCaseSource(nameof(TestCases))]
Expand All @@ -118,31 +131,39 @@ public void CompatibilityDecompressBase64FromNode(LZStringTestCase test)
{
Assert.Fail($"Invalid Base64 string: '{compress}'{Environment.NewLine}{exc.Message}");
}
Assert.That(uncompress, Is.EqualTo(test.Uncompressed), $"Compression result: {compress}");
Console.WriteLine("lz-string compression result:");
Console.WriteLine(compress);
Assert.That(uncompress, Is.EqualTo(test.Uncompressed));
}

[TestCaseSource(nameof(TestCases))]
public void CompatibilityCompressBase64FromCSharp(LZStringTestCase test)
{
var compress = LZString.CompressToBase64(test.Uncompressed);
var uncompress = RunNodeLzString("decompressFromBase64", compress);
Assert.That(uncompress, Is.EqualTo(test.Uncompressed), $"Compression result: {compress}");
Console.WriteLine("c# compression result:");
Console.WriteLine(compress);
Assert.That(uncompress, Is.EqualTo(test.Uncompressed));
}

[TestCaseSource(nameof(TestCases))]
public void CompatibilityDecompressUTF16FromNode(LZStringTestCase test)
{
var compress = RunNodeLzString("compressToUTF16", test.Uncompressed);
var uncompress = LZString.DecompressFromUTF16(compress);
Assert.That(uncompress, Is.EqualTo(test.Uncompressed), $"Compression result: {compress}");
Console.WriteLine("lz-string compression result:");
Console.WriteLine(compress);
Assert.That(uncompress, Is.EqualTo(test.Uncompressed));
}

[TestCaseSource(nameof(TestCases))]
public void CompatibilityCompressUTF16FromCSharp(LZStringTestCase test)
{
var compress = LZString.CompressToUTF16(test.Uncompressed);
var uncompress = RunNodeLzString("decompressFromUTF16", compress);
Assert.That(uncompress, Is.EqualTo(test.Uncompressed), $"Compression result: {compress}");
Console.WriteLine("c# compression result:");
Console.WriteLine(compress);
Assert.That(uncompress, Is.EqualTo(test.Uncompressed));
}

public struct LZStringTestCase
Expand Down

0 comments on commit 3e4b641

Please sign in to comment.