Files
workbook/tests/gnumeric_test.zi

259 lines
17 KiB
Ziran

// Imported documents use Gnumeric for interpretation and round trips. The
// fixture is synthetic; no owner workbooks belong in the repository.
#load "test_support.zi";
Engine :: #import "engine";
ExpectSource :: (book: *Engine.Workbook, sheet: s32, row: s32, col: s32, text: string) {
Check(Engine.TextCompareLiteral(Engine.WorkbookCellText(book, sheet, row, col), text) == 0,
"imported cell source differs")
}
ExpectDisplay :: (book: *Engine.Workbook, sheet: s32, row: s32, col: s32, text: string) {
output: [256]s8
Engine.WorkbookCellDisplay(book, sheet, row, col, *output[0], 256)
if Engine.TextCompareLiteral(*output[0], text) != 0 {
SayWith("Expected: ", text)
SayWith("Actual: ", TextFromCString(cast(*u8)*output[0]))
Fail("Gnumeric display differs")
}
}
ExpectHidden :: (book: *Engine.Workbook, sheet: s32, row: s32, expected: s32) {
Check(Engine.WorkbookRowHidden(book, sheet, row) == expected, "native hidden row state differs")
}
#program_export
main :: () -> s32 {
book: *Engine.Workbook = Engine.WorkbookNew()
Check(Engine.WorkbookLoadFile(book, Engine.CText("tests/document.gnumeric")) != 0, "cannot import Gnumeric document")
Check(book.native != null && Engine.WorkbookSheetCount(book) == 2, "native document not retained")
ExpectSource(book, 0, 2, 3, "=(C3-B3)*24")
ExpectSource(book, 0, 100000, 300, "42")
ExpectDisplay(book, 0, 1, 0, "2026-01-05")
ExpectDisplay(book, 0, 2, 3, "1.50")
ExpectDisplay(book, 0, 3, 3, "2.50")
ExpectDisplay(book, 0, 4, 3, "2.50")
ExpectDisplay(book, 1, 0, 0, "=literal & text")
ExpectDisplay(book, 1, 0, 1, "000123")
ExpectDisplay(book, 1, 1, 0, "TRUE")
ExpectDisplay(book, 0, 7, 0, "1")
ExpectDisplay(book, 0, 8, 0, "2")
// General numbers use the grid's two decimals, not the column's width.
ExpectDisplay(book, 0, 5, 4, "1.14")
ExpectDisplay(book, 0, 5, 5, "-0.00011")
Check(Engine.WorkbookSetCell(book, 0, 6, 4, Engine.CText("0.05")) != 0, "cannot type a fraction")
ExpectDisplay(book, 0, 6, 4, "0.05")
Check(Engine.WorkbookSetCell(book, 0, 6, 5, Engine.CText("=E7*528.44")) != 0, "cannot multiply a typed fraction")
ExpectDisplay(book, 0, 6, 5, "26.42")
Check(Engine.WorkbookSetCell(book, 0, 6, 5, Engine.CText("=E7*2000")) != 0, "cannot compute a whole number")
ExpectDisplay(book, 0, 6, 5, "100")
Check(Engine.WorkbookSetCell(book, 0, 6, 4, Engine.CText("")) != 0, "cannot clear a typed fraction")
Check(Engine.WorkbookSetCell(book, 0, 6, 5, Engine.CText("")) != 0, "cannot clear a product")
Check(Engine.WorkbookSetCell(book, 0, 8, 0, Engine.CText("99")) == 0, "part of an array was overwritten")
Check(Engine.WorkbookSetRowsHidden(book, 0, 2, 3, 1) != 0, "cannot hide native rows")
ExpectHidden(book, 0, 2, 1)
ExpectHidden(book, 0, 3, 1)
ExpectHidden(book, 0, 1, 0)
ExpectHidden(book, 0, 4, 0)
copy: *Engine.Workbook = Engine.WorkbookClone(book)
Check(copy != null, "native undo snapshot failed")
ExpectHidden(copy, 0, 2, 1)
ExpectHidden(copy, 0, 3, 1)
Check(Engine.WorkbookSetCell(book, 0, 2, 2, Engine.CText("11:15")) != 0, "native edit failed")
ExpectDisplay(book, 0, 2, 3, "2.00")
ExpectDisplay(book, 0, 4, 3, "3.00")
ExpectDisplay(copy, 0, 2, 3, "1.50")
Check(Engine.WorkbookInsertRow(book, 0, 0) != 0, "native row insertion failed")
ExpectDisplay(book, 0, 2, 0, "2026-01-05")
ExpectDisplay(book, 0, 5, 3, "3.00")
Check(Engine.WorkbookDeleteRow(book, 0, 0) != 0, "native row deletion failed")
ExpectDisplay(book, 0, 1, 0, "2026-01-05")
template: [4096]u8
SetText(template[:], "/tmp/workbook-gnumeric-XXXXXX")
work: string = MakeTemporaryDirectory(template[:])
Check(work.count > 0, "cannot create test output directory")
SetText(cleanup_directory[:], work)
path: [4096]u8
SetText(path[:], work)
AppendText(path[:], "/saved.gnumeric")
Check(Engine.WorkbookWriteGnumeric(book, cast(*s8)*path[0]) != 0, "native save failed")
saved: *Engine.Workbook = Engine.WorkbookNew()
Check(Engine.WorkbookLoadFile(saved, cast(*s8)*path[0]) != 0, "native round trip failed")
ExpectDisplay(saved, 0, 1, 0, "2026-01-05")
ExpectDisplay(saved, 0, 2, 3, "2.00")
ExpectDisplay(saved, 0, 4, 3, "3.00")
ExpectSource(saved, 0, 100000, 300, "42")
ExpectDisplay(saved, 1, 0, 1, "000123")
ExpectDisplay(saved, 0, 8, 0, "2")
ExpectHidden(saved, 0, 2, 1)
ExpectHidden(saved, 0, 3, 1)
saved_xml: [262144]u8
saved_length: s32 = ReadWholeFile(TextUntilNul(path[:]), saved_xml[:])
Check(saved_length > 0 && saved_length < cast(s32)saved_xml.count, "cannot read the saved native document")
if saved_length > 0 && saved_length < cast(s32)saved_xml.count {
saved_xml[saved_length] = 0
document: *u8 = *saved_xml[0]
inflated: u64 = 0
if Engine.GzipIs(document, cast(u64)saved_length) != 0 {
document = Engine.GzipDecompress(document, cast(u64)saved_length, *inflated)
}
Check(document != null, "saved native document could not be decoded")
if document != null {
Check(Engine.TextFindLiteral(cast(*s8)document, "Hidden=\"1\"") != null, "saved native document omitted hidden rows")
if document != *saved_xml[0] { RawFree(cast(*void)document) }
}
}
Check(Engine.WorkbookSetRowsHidden(saved, 0, 2, 3, 0) != 0, "cannot show native rows")
ExpectHidden(saved, 0, 2, 0)
ExpectHidden(saved, 0, 3, 0)
subtotals: *Engine.Workbook = Engine.WorkbookNew()
Engine.WorkbookAddSheet(subtotals, Engine.CText("Sheet1"))
Engine.WorkbookAddSheet(subtotals, Engine.CText("Sheet2"))
Check(Engine.WorkbookSetCell(subtotals, 0, 0, 0, Engine.CText("1")) != 0, "cannot set a native subtotal value")
Check(Engine.WorkbookSetCell(subtotals, 0, 1, 0, Engine.CText("2")) != 0, "cannot set a native subtotal value")
Check(Engine.WorkbookSetCell(subtotals, 0, 2, 0, Engine.CText("3")) != 0, "cannot set a native subtotal value")
Check(Engine.WorkbookSetCell(subtotals, 0, 3, 0, Engine.CText("4")) != 0, "cannot set a native subtotal value")
Check(Engine.WorkbookSetCell(subtotals, 0, 4, 0, Engine.CText("5")) != 0, "cannot set a native subtotal value")
Check(Engine.WorkbookSetCell(subtotals, 0, 5, 0, Engine.CText("=SUBTOTAL(9,A1:A5)")) != 0, "cannot set a nested native subtotal")
Check(Engine.WorkbookSetCell(subtotals, 0, 0, 1, Engine.CText("=SUBTOTAL(109,A1:A5)")) != 0, "cannot set a visible native sum")
Check(Engine.WorkbookSetCell(subtotals, 0, 1, 1, Engine.CText("=SUBTOTAL(9,A1:A5)")) != 0, "cannot set an all-row native sum")
Check(Engine.WorkbookSetCell(subtotals, 0, 0, 3, Engine.CText("1")) != 0, "cannot set a native matrix value")
Check(Engine.WorkbookSetCell(subtotals, 0, 0, 4, Engine.CText("2")) != 0, "cannot set a native matrix value")
Check(Engine.WorkbookSetCell(subtotals, 0, 1, 3, Engine.CText("3")) != 0, "cannot set a native matrix value")
Check(Engine.WorkbookSetCell(subtotals, 0, 1, 4, Engine.CText("4")) != 0, "cannot set a native matrix value")
Check(Engine.WorkbookSetCell(subtotals, 0, 2, 3, Engine.CText("5")) != 0, "cannot set a native matrix value")
Check(Engine.WorkbookSetCell(subtotals, 0, 2, 4, Engine.CText("6")) != 0, "cannot set a native matrix value")
Check(Engine.WorkbookSetCell(subtotals, 0, 2, 1, Engine.CText("=SUBTOTAL(109,D1:E3)")) != 0, "cannot set a native matrix subtotal")
Check(Engine.WorkbookSetCell(subtotals, 1, 0, 0, Engine.CText("10")) != 0, "cannot set a cross-sheet subtotal value")
Check(Engine.WorkbookSetCell(subtotals, 1, 1, 0, Engine.CText("20")) != 0, "cannot set a cross-sheet subtotal value")
Check(Engine.WorkbookSetCell(subtotals, 1, 2, 0, Engine.CText("30")) != 0, "cannot set a cross-sheet subtotal value")
Check(Engine.WorkbookSetCell(subtotals, 0, 3, 1, Engine.CText("=SUBTOTAL(109,Sheet2!A1:A3)")) != 0, "cannot set a cross-sheet subtotal")
Check(Engine.WorkbookSetCell(subtotals, 0, 4, 1, Engine.CText("=SUBTOTAL(109,A1:A6)")) != 0, "cannot set an outer native subtotal")
Check(Engine.WorkbookSetCell(subtotals, 0, 5, 1, Engine.CText("=SUBTOTAL(109,A1:A5,100)")) != 0, "cannot set a multi-argument native subtotal")
Check(Engine.WorkbookSetCell(subtotals, 0, 6, 1, Engine.CText("=SUBTOTAL(101,A1:A5)")) != 0, "cannot set a native visible average")
Check(Engine.WorkbookSetCell(subtotals, 0, 7, 1, Engine.CText("=SUBTOTAL(102,A1:A5)")) != 0, "cannot set a native visible count")
Check(Engine.WorkbookSetCell(subtotals, 0, 8, 1, Engine.CText("=SUBTOTAL(109,A2)")) != 0, "cannot set a direct native subtotal")
Check(Engine.WorkbookSetCell(subtotals, 0, 9, 1, Engine.CText("=SUBTOTAL(109,Sheet2!A2)")) != 0, "cannot set a direct cross-sheet native subtotal")
Check(Engine.WorkbookSetCell(subtotals, 0, 10, 1, Engine.CText("=SUBTOTAL(109,A6)")) != 0, "cannot set a direct nested subtotal")
Check(Engine.WorkbookSetCell(subtotals, 0, 11, 1, Engine.CText("=SUBTOTAL(9,A6)")) != 0, "cannot set a direct nested all-row subtotal")
Check(Engine.WorkbookSetCell(subtotals, 0, 12, 1, Engine.CText("=SUBTOTAL(104,J1:J2)")) != 0, "cannot set an empty maximum subtotal")
Check(Engine.WorkbookSetCell(subtotals, 0, 13, 1, Engine.CText("=SUBTOTAL(101,J1:J2)")) != 0, "cannot set an empty average subtotal")
SetText(path[:], work); AppendText(path[:], "/subtotals-source.gnumeric")
Check(Engine.WorkbookWriteGnumeric(subtotals, cast(*s8)*path[0]) != 0, "cannot create a native subtotal fixture")
subtotal_source: *Engine.Workbook = subtotals
subtotals = Engine.WorkbookNew()
Check(Engine.WorkbookLoadFile(subtotals, cast(*s8)*path[0]) != 0 && subtotals.native != null, "cannot import the native subtotal fixture")
Engine.WorkbookFree(subtotal_source)
Check(Engine.WorkbookSetCell(subtotals, 0, 0, 9, Engine.CText("'=SUBTOTAL(9,A1:A5)")) != 0, "cannot set literal subtotal text")
Check(Engine.WorkbookSetCell(subtotals, 0, 14, 1, Engine.CText("=SUBTOTAL(103,J1)")) != 0, "cannot count literal subtotal text")
ExpectDisplay(subtotals, 0, 14, 1, "1")
ExpectDisplay(subtotals, 0, 0, 1, "15")
ExpectDisplay(subtotals, 0, 1, 1, "15")
ExpectDisplay(subtotals, 0, 2, 1, "21")
ExpectDisplay(subtotals, 0, 3, 1, "60")
ExpectDisplay(subtotals, 0, 4, 1, "15")
ExpectDisplay(subtotals, 0, 5, 1, "115")
ExpectDisplay(subtotals, 0, 6, 1, "3")
ExpectDisplay(subtotals, 0, 7, 1, "5")
ExpectDisplay(subtotals, 0, 8, 1, "2")
ExpectDisplay(subtotals, 0, 9, 1, "20")
ExpectDisplay(subtotals, 0, 10, 1, "0")
ExpectDisplay(subtotals, 0, 11, 1, "0")
ExpectDisplay(subtotals, 0, 12, 1, "0")
ExpectDisplay(subtotals, 0, 13, 1, "#DIV/0!")
Check(Engine.WorkbookSetRowsHidden(subtotals, 0, 0, 0, 1) != 0, "cannot hide a native subtotal row")
Check(Engine.WorkbookSetRowsHidden(subtotals, 0, 2, 2, 1) != 0, "cannot hide another native subtotal row")
Check(Engine.WorkbookSetRowsHidden(subtotals, 1, 1, 1, 1) != 0, "cannot hide a cross-sheet subtotal row")
ExpectDisplay(subtotals, 0, 0, 1, "11")
ExpectDisplay(subtotals, 0, 1, 1, "15")
ExpectDisplay(subtotals, 0, 2, 1, "7")
ExpectDisplay(subtotals, 0, 3, 1, "40")
ExpectDisplay(subtotals, 0, 4, 1, "11")
ExpectDisplay(subtotals, 0, 5, 1, "111")
ExpectDisplay(subtotals, 0, 6, 1, "3.67")
ExpectDisplay(subtotals, 0, 7, 1, "3")
ExpectDisplay(subtotals, 0, 8, 1, "2")
ExpectDisplay(subtotals, 0, 9, 1, "0")
Check(Engine.WorkbookSetRowsHidden(subtotals, 0, 1, 1, 1) != 0, "cannot hide a direct native subtotal row")
ExpectDisplay(subtotals, 0, 8, 1, "0")
Check(Engine.WorkbookSetRowsHidden(subtotals, 1, 1, 1, 1) != 0, "cannot hide a direct cross-sheet subtotal row again")
ExpectDisplay(subtotals, 0, 9, 1, "0")
Check(Engine.WorkbookSetRowsHidden(subtotals, 0, 5, 5, 1) != 0, "cannot hide a native subtotal formula row")
ExpectDisplay(subtotals, 0, 5, 1, "109")
Check(Engine.WorkbookSetRowsHidden(subtotals, 0, 5, 5, 0) != 0, "cannot show a native subtotal formula row")
Check(Engine.WorkbookSetRowsHidden(subtotals, 1, 1, 1, 0) != 0, "cannot show a direct cross-sheet subtotal row")
ExpectDisplay(subtotals, 0, 9, 1, "20")
Check(Engine.WorkbookSetRowsHidden(subtotals, 0, 1, 1, 0) != 0, "cannot show a direct native subtotal row")
ExpectDisplay(subtotals, 0, 8, 1, "2")
subtotal_copy: *Engine.Workbook = Engine.WorkbookClone(subtotals)
Check(subtotal_copy != null, "cannot clone native subtotal state")
if subtotal_copy != null {
ExpectHidden(subtotal_copy, 0, 0, 1)
ExpectDisplay(subtotal_copy, 0, 0, 1, "11")
}
SetText(path[:], work); AppendText(path[:], "/subtotals.gnumeric")
Check(Engine.WorkbookWriteGnumeric(subtotals, cast(*s8)*path[0]) != 0, "cannot save native subtotals")
subtotal_saved: *Engine.Workbook = Engine.WorkbookNew()
Check(Engine.WorkbookLoadFile(subtotal_saved, cast(*s8)*path[0]) != 0, "cannot reload native subtotals")
ExpectSource(subtotal_saved, 0, 0, 1, "=subtotal(109,A1:A5)")
ExpectHidden(subtotal_saved, 0, 0, 1)
ExpectDisplay(subtotal_saved, 0, 0, 1, "11")
Check(Engine.WorkbookSetRowsHidden(subtotals, 0, 0, 0, 0) != 0, "cannot show a native subtotal row")
ExpectDisplay(subtotals, 0, 0, 1, "12")
Check(Engine.WorkbookSetRowsHidden(subtotals, 0, 2, 2, 0) != 0, "cannot show another native subtotal row")
ExpectDisplay(subtotals, 0, 0, 1, "15")
ExpectDisplay(subtotals, 0, 6, 1, "3")
ExpectDisplay(subtotals, 0, 7, 1, "5")
Check(Engine.WorkbookSetRowsHidden(subtotals, 1, 1, 1, 0) != 0, "cannot show a cross-sheet subtotal row")
ExpectDisplay(subtotals, 0, 3, 1, "60")
Engine.WorkbookFree(subtotal_saved)
if subtotal_copy != null { Engine.WorkbookFree(subtotal_copy) }
Engine.WorkbookFree(subtotals)
// A native document is not a NUL-terminated UTF-8 string. UTF-16 XML
// contains embedded zero bytes and must reach Gnumeric intact.
xml: string = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><Workbook xmlns=\"http://www.gnumeric.org/v10.dtd\"><Sheets><Sheet><Name>Wide</Name><Cells><Cell Row=\"0\" Col=\"0\" ValueType=\"40\">42</Cell></Cells></Sheet></Sheets></Workbook>"
wide: [4096]u8
wide[0] = 255; wide[1] = 254
for at: 0..cast(s32)xml.count - 1 { wide[2 + at * 2] = xml[at]; wide[3 + at * 2] = 0 }
SetText(path[:], work); AppendText(path[:], "/encoded.gnumeric")
Check(WriteWholeFile(TextUntilNul(path[:]), TextView(wide[0:2 + cast(s32)xml.count * 2])), "cannot write UTF-16 fixture")
encoded: *Engine.Workbook = Engine.WorkbookNew()
Check(Engine.WorkbookLoadFile(encoded, cast(*s8)*path[0]) != 0, "UTF-16 document was truncated")
ExpectDisplay(encoded, 0, 0, 0, "42")
Engine.WorkbookFree(encoded)
// The independent writer also declares enough grid space when a new
// workbook reaches beyond the original 256-column/65,536-row default.
fresh: *Engine.Workbook = Engine.WorkbookNew()
Engine.WorkbookAddSheet(fresh, Engine.CText("Large"))
Check(Engine.WorkbookSetCell(fresh, 0, 100000, 300, Engine.CText("42")) != 0, "cannot create a large-coordinate cell")
SetText(path[:], work); AppendText(path[:], "/large.gnumeric")
Check(Engine.WorkbookWriteGnumeric(fresh, cast(*s8)*path[0]) != 0, "cannot write large dimensions")
imported: *Engine.Workbook = Engine.WorkbookNew()
Check(Engine.WorkbookLoadFile(imported, cast(*s8)*path[0]) != 0, "cannot re-read large dimensions")
ExpectSource(imported, 0, 100000, 300, "42")
Engine.WorkbookFree(imported)
Engine.WorkbookFree(fresh)
// CSV's first field can begin with '<'; it must not be mistaken for XML.
SetText(path[:], work); AppendText(path[:], "/literal.csv")
Check(WriteWholeFile(TextUntilNul(path[:]), "<not xml,42\n"), "cannot write CSV fixture")
table: *Engine.Workbook = Engine.WorkbookNew()
Check(Engine.WorkbookLoadFile(table, cast(*s8)*path[0]) != 0 && table.native == null, "CSV was routed to the Gnumeric reader")
ExpectSource(table, 0, 0, 0, "<not xml")
ExpectDisplay(table, 0, 0, 1, "42")
Engine.WorkbookFree(table)
compressed: [32]u8 = .{31, 139, 8, 0, 0, 0, 0, 0, 2, 255, 179, 201, 203, 47, 81, 168, 200, 205, 209, 49, 49, 226, 2, 0, 135, 87, 141, 239, 12, 0, 0, 0}
SetText(path[:], work); AppendText(path[:], "/literal.csv.gz")
Check(WriteWholeFile(TextUntilNul(path[:]), TextView(compressed[:])), "cannot write compressed CSV fixture")
table = Engine.WorkbookNew()
Check(Engine.WorkbookLoadFile(table, cast(*s8)*path[0]) != 0 && table.native == null, "compressed CSV changed behavior")
ExpectSource(table, 0, 0, 0, "<not xml")
ExpectDisplay(table, 0, 0, 1, "42")
Engine.WorkbookFree(table)
Engine.WorkbookFree(saved)
Engine.WorkbookFree(copy)
Engine.WorkbookFree(book)
RemoveTree(work)
cleanup_directory[0] = 0
Say("Gnumeric: shared formulas, time arithmetic, names, formatting, arrays, large coordinates, edits, structure, undo and round trips passed")
return 0
}