Skip to content

Commit 2fc4987

Browse files
committed
Improve SmartCN tests: Replace file existence checks with asserts, refine maxlength usage
1 parent cb6d24c commit 2fc4987

3 files changed

Lines changed: 21 additions & 23 deletions

File tree

src/Lucene.Net.Analysis.SmartCn/Hhmm/BigramDictionary.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,12 @@ public virtual void LoadFromFile(string dctFilePath)
266266
Span<int> buffer = stackalloc int[3];
267267
string tmpword;
268268

269-
// LUCENENET: Removed buffer and intBuffer arrays since BinaryReader handles reading values directly in a more type-safe and readable way.
269+
// LUCENENET: Removed intBuffer arrays since BinaryReader handles reading values directly in a more type-safe and readable way.
270270
// LUCENENET specific - refactored constants for clarity
271271

272272
// The 3756th position (using 1-based counting) corresponds to index 3755 (using 0-based indexing)
273273
// This matches the original Java implementation which used 3755 + GB2312_FIRST_CHAR in the condition
274274
const int HEADER_POSITION = 3755;
275-
const int MAX_VALID_LENGTH = 1000;
276275

277276
//using (RandomAccessFile dctFile = new RandomAccessFile(dctFilePath, "r"))
278277
using var dctFile = new FileStream(dctFilePath, FileMode.Open, FileAccess.Read);
@@ -310,7 +309,7 @@ public virtual void LoadFromFile(string dctFilePath)
310309
buffer[2] = reader.ReadInt32(); // Skip handle value (unused)
311310

312311
length = buffer[1];
313-
if (length > 0 && length <= MAX_VALID_LENGTH && dctFile.Position + length <= dctFile.Length)
312+
if (length > 0 && dctFile.Position + length <= dctFile.Length)
314313
{
315314
byte[] lchBuffer = reader.ReadBytes(length); // LUCENENET: Use BinaryReader to decode little endian instead of ByteBuffer, since this is the default in .NET
316315

src/Lucene.Net.Analysis.SmartCn/Hhmm/WordDictionary.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,9 @@ private int LoadMainDataFromFile(string dctFilePath)
351351
// The 3756th is used (as a header) to store information.
352352

353353
Span<int> buffer = stackalloc int[3];
354+
string tmpword;
354355

355-
// LUCENENET: Removed buffer and intBuffer arrays since BinaryReader handles reading values directly in a more type-safe and readable way.
356+
// LUCENENET: Removed intBuffer arrays since BinaryReader handles reading values directly in a more type-safe and readable way.
356357
// LUCENENET: Use BinaryReader to simplify endian conversion and stream reading.
357358

358359
using (var dctFile = new FileStream(dctFilePath, FileMode.Open, FileAccess.Read))
@@ -394,7 +395,7 @@ private int LoadMainDataFromFile(string dctFilePath)
394395
if (length > 0)
395396
{
396397
byte[] lchBuffer = reader.ReadBytes(length);
397-
string tmpword = gb2312Encoding.GetString(lchBuffer); // LUCENENET: Use cached encoding instance from base class
398+
tmpword = gb2312Encoding.GetString(lchBuffer); // LUCENENET: Use cached encoding instance from base class
398399
wordItem_charArrayTable[i][j] = tmpword.ToCharArray();
399400
}
400401
else

src/Lucene.Net.Tests.Analysis.SmartCn/Hhmm/TestBuildDictionary.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,22 @@ public void TestBigramDictionary()
5555
// First test - builds and loads dictionary from .dict file
5656
BigramDictionary bigramDict = BigramDictionary.GetInstance();
5757
CheckBigramDictionary(bigramDict);
58-
58+
5959
// Ensure .mem file was created
60-
string memFile = System.IO.Path.Combine(tempDir.FullName, "bigramdict.mem");
60+
string memFile = System.IO.Path.Combine(tempDir.FullName, "bigramdict.mem");
6161
Assert.IsTrue(File.Exists(memFile), "Memory file should be created after first load");
62-
62+
6363
// Delete the original .dict file
64-
string dictFile = System.IO.Path.Combine(tempDir.FullName, "bigramdict.dct");
65-
if (File.Exists(dictFile))
66-
{
67-
File.Delete(dictFile);
68-
}
69-
64+
string dictFile = System.IO.Path.Combine(tempDir.FullName, "bigramdict.dct");
65+
Assert.IsTrue(File.Exists(dictFile), $"{dictFile} does not exist.");
66+
File.Delete(dictFile);
67+
68+
7069
// Second test - should load from .mem file now
7170
bigramDict = BigramDictionary.GetInstance();
7271
CheckBigramDictionary(bigramDict);
7372
}
74-
73+
7574
private static void CheckBigramDictionary(BigramDictionary bigramDict)
7675
{
7776
Assert.AreEqual(10, bigramDict.GetFrequency("啊hello".AsSpan()), "Frequency for '啊hello' is incorrect.");
@@ -84,23 +83,22 @@ public void TestWordDictionary()
8483
// First test - builds and loads dictionary from .dict file
8584
WordDictionary wordDict = WordDictionary.GetInstance();
8685
CheckWordDictionary(wordDict);
87-
86+
8887
// Ensure .mem file was created
8988
string memFile = System.IO.Path.Combine(tempDir.FullName, "coredict.mem");
9089
Assert.IsTrue(File.Exists(memFile), "Memory file should be created after first load");
91-
90+
9291
// Delete the original .dict file
9392
string dictFile = System.IO.Path.Combine(tempDir.FullName, "coredict.dct");
94-
if (File.Exists(dictFile))
95-
{
96-
File.Delete(dictFile);
97-
}
98-
93+
Assert.IsTrue(File.Exists(dictFile), $"{dictFile} does not exist.");
94+
File.Delete(dictFile);
95+
96+
9997
// Second test - should load from .mem file now
10098
wordDict = WordDictionary.GetInstance();
10199
CheckWordDictionary(wordDict);
102100
}
103-
101+
104102
private static void CheckWordDictionary(WordDictionary wordDict)
105103
{
106104
Assert.AreEqual(30, wordDict.GetFrequency("尼".ToCharArray()), "Frequency for '尼' is incorrect.");

0 commit comments

Comments
 (0)