From 8da67c9c0ac537ba184d22b16f86d2d1b18f17ab Mon Sep 17 00:00:00 2001 From: Heinrich Lukas Weil Date: Fri, 2 Feb 2024 13:59:17 +0100 Subject: [PATCH] small fixes and adjustments to xlsx reader and tests --- src/FsSpreadsheet/FsAddress.fs | 7 +++---- .../ZipArchiveReader.fs | 12 +++++++----- tests/FsSpreadsheet.Tests/FsWorksheetTests.fs | 13 +++++++++++++ tests/Speedtest/Program.fs | 4 +++- tests/TestUtils/TestingUtils.fs | 14 ++++++++++++++ 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/FsSpreadsheet/FsAddress.fs b/src/FsSpreadsheet/FsAddress.fs index 2659c475..872d28bd 100644 --- a/src/FsSpreadsheet/FsAddress.fs +++ b/src/FsSpreadsheet/FsAddress.fs @@ -37,13 +37,12 @@ module CellReference = let ofIndices column (row : uint32) = sprintf "%s%i" (indexToColAdress column) row - let private charPart = System.Text.StringBuilder() - let private numPart = System.Text.StringBuilder() + /// Maps a "A1" style excel cell reference to a column * row index tuple (1 Based indices). let toIndices (reference : string) = - let charPart = charPart.Clear() - let numPart = numPart.Clear() + let charPart = System.Text.StringBuilder() + let numPart = System.Text.StringBuilder() reference |> Seq.iter (fun c -> diff --git a/tests/FsSpreadsheet.ExcelIO.Tests/ZipArchiveReader.fs b/tests/FsSpreadsheet.ExcelIO.Tests/ZipArchiveReader.fs index 2ba63bd9..8ac28337 100644 --- a/tests/FsSpreadsheet.ExcelIO.Tests/ZipArchiveReader.fs +++ b/tests/FsSpreadsheet.ExcelIO.Tests/ZipArchiveReader.fs @@ -1,6 +1,5 @@ module ZipArchiveReader -open Expecto open TestingUtils open FsSpreadsheet open FsSpreadsheet.ExcelIO.ZipArchiveReader @@ -35,15 +34,18 @@ let tests_Read = testList "Read" [ Expect.isDefaultTestObject wb ] +open FsSpreadsheet.ExcelIO + let performanceTest = testList "Performance" [ testCase "BigFile" <| fun _ -> - let readF() = FsWorkbook.fromFile("./TestFiles/BigFile.xlsx") - let wb = Expect.wantFaster readF 5 "Reader too slow" - Expect.equal (wb.GetWorksheetAt(1).Rows.Count) 153991 "Row count should be equal" + let readF() = FsWorkbook.fromFile("./TestFiles/BigFile.xlsx") |> ignore + let refReadF() = FsWorkbook.fromXlsxFile("./TestFiles/BigFile.xlsx") |> ignore + Expect.isFasterThan readF refReadF "ZipArchiveReader should be faster than standard reader" + //Expect.equal (wb.GetWorksheetAt(1).Rows.Count) 153991 "Row count should be equal" ] -[] +[] let main = testList "ZipArchiveReader" [ performanceTest tests_Read diff --git a/tests/FsSpreadsheet.Tests/FsWorksheetTests.fs b/tests/FsSpreadsheet.Tests/FsWorksheetTests.fs index e621ed10..0e8c7400 100644 --- a/tests/FsSpreadsheet.Tests/FsWorksheetTests.fs +++ b/tests/FsSpreadsheet.Tests/FsWorksheetTests.fs @@ -38,9 +38,22 @@ let tests_SortRows = testList "SortRows" [ Utils.Expect.mySequenceEqual ws.Rows rows "equal" ] +let tests_rescanRows = testList "RescanRows" [ + testCase "empty" <| fun _ -> + dummySheet1.RescanRows() + Expect.hasLength dummySheet1.Rows 0 "row count" + testCase "rows" <| fun _ -> + let ws = createBigDummySheet() + let rows = ResizeArray(ws.Rows) // create copy + ws.RescanRows() + Utils.Expect.mySequenceEqual ws.Rows rows "equal" +] + + let main = testSequenced <| testList "FsWorksheet" [ tests_SortRows + tests_rescanRows testList "FsCell data" [ // TO DO: Ask TM: useful? or was that a mistake? (since the same test is seen in FsCell.fs) testList "Data | DataType | Adress" [ diff --git a/tests/Speedtest/Program.fs b/tests/Speedtest/Program.fs index 31504dd6..acb53bef 100644 --- a/tests/Speedtest/Program.fs +++ b/tests/Speedtest/Program.fs @@ -27,11 +27,13 @@ let main argv = let readAssay() = ZipArchiveReader.FsWorkbook.fromFile assayPath let readStudy() = ZipArchiveReader.FsWorkbook.fromFile studyPath let readInvestigation() = ZipArchiveReader.FsWorkbook.fromFile investigationPath + let bigFile() = ZipArchiveReader.FsWorkbook.fromFile @"C:\Users\HLWei\source\repos\IO\FsSpreadsheet\tests\TestUtils\TestFiles\BigFile.xlsx" + readInvestigation() |> ignore readAssay() |> ignore readStudy() |> ignore - + bigFile() |> ignore let closedXML() = diff --git a/tests/TestUtils/TestingUtils.fs b/tests/TestUtils/TestingUtils.fs index 66a74eb1..063cf1de 100644 --- a/tests/TestUtils/TestingUtils.fs +++ b/tests/TestUtils/TestingUtils.fs @@ -83,6 +83,20 @@ module Expect = failwithf $"{message}. Expected to be faster than {maxMilliseconds}ms, but took {elapsed.TotalMilliseconds}ms" res + let isFasterThan (f1 : unit -> _) (f2 : unit -> _) (message : string) = + let stopwatch = Stopwatch() + stopwatch.Start() + f1() + stopwatch.Stop() + let elapsed1 = stopwatch.Elapsed + stopwatch.Start() + f2() + stopwatch.Stop() + let elapsed2 = stopwatch.Elapsed + if elapsed1.TotalMilliseconds > elapsed2.TotalMilliseconds then + failwithf $"{message}. Expected {elapsed1.TotalMilliseconds}ms to be faster than {elapsed2.TotalMilliseconds}ms" + () + let cellSequenceEquals (actual: FsCell seq) (expected: FsCell seq) message = let cellDiff (s1: FsCell seq) (s2: FsCell seq) = let s1 = Seq.append (Seq.map Some s1) (Seq.initInfinite (fun _ -> None))