Skip to content

lib/segment: Add pytest ctypes test suite - #8316

Open
jalpatel11 wants to merge 1 commit into
OSGeo:mainfrom
jalpatel11:add-lib-segment-ctypes-tests
Open

jalpatel11 wants to merge 1 commit into
OSGeo:mainfrom
jalpatel11:add-lib-segment-ctypes-tests

Conversation

@jalpatel11

@jalpatel11 jalpatel11 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

lib/segment is the paging library that lets tools work on data larger than memory by keeping a matrix in a file and only a few segments of it in RAM. It is used by r.cost, r.walk, r.watershed (segmented mode), r.stream.extract, i.segment, v.surf.bspline and others, but it currently has no tests of any kind. This PR adds a pytest suite that calls the C API directly through the existing grass.lib.segment ctypes bindings, continuing the test coverage work from #7871, #7879 and #7919.

The PR adds 95 tests (16 test functions) in one file, along with a conftest.py for the fixtures.

Each test checks behavior documented in the doc comments of lib/segment/*.c or in segmentlib.dox. The one exception is a check that the parametrized layouts really exercise the code paths they are named for.

lib_segment_ctypes_test.py

Random access through Segment_open()

  • Segment_put() and Segment_get() round trip for every cell
  • Values surviving page-out and page-in with a single segment in memory (nseg = 1)
  • Segment_put_row() and Segment_get_row() round trip for whole rows
  • Segment_flush() before the first Segment_get_row(), and its documented return value of 0

These tests run in both modes of Segment_open(): paging to a file, and keeping the whole matrix in memory when nseg covers every segment.

They are also parametrized over 7 matrix and segment shapes. setup.c uses a shift-and-mask address calculation when srows and scols are powers of two, and a faster seek when the value length is one too, so the shapes cover the fast, partly fast and slow paths. Two of the shapes are ragged, so their last segments are only partly used.

Each cell holds a value derived from its row and column, with no zero bytes. A value written to the wrong cell, or copied one byte short, is detected, not only a missing value.

Opening and closing

  • Segment_open() returning -1 for a NULL file name and for a file that already exists, leaving the existing file untouched
  • Segment_open() returning -3 for an illegal value length, and removing the file it created
  • Segment_close() deleting the segment file, and returning -1 for a segment that is not open

Low-level API

  • Segment_format() zero-filling the file, then Segment_init(), put/get and Segment_release(). The file is filled with 0xff first, so the zero check is meaningful.
  • Segment_format() and Segment_format_nofill() returning -3 for each non-positive parameter
  • Segment_init() returning -1 for a file that is empty or shorter than a header
  • Segment_release() returning -1 for a segment that is not open

conftest.py

The segment functions call G_debug(), and G_warning() on some failure paths, but never touch a project or mapset. Instead of creating a GRASS session, the fixtures:

  • Switch GISRC to memory mode and restore it afterwards, and initialize libgis without a mapset, as g.message and g.dirseps do. ctypesgen does not translate the G_no_gisinit() macro, so the fixture calls G__no_gisinit(GIS_H_VERSION). On Windows this also makes files open in binary mode, which Segment_open() needs.
  • Register a no-op handler with G_set_error_routine(), following gui/wxpython/vdigit/wxdisplay.py, so that G_warning() does not exit the process when GISBASE is unset.

The low-level tests pass a file descriptor to the library. On Windows, Python and the GRASS libraries use different C runtimes with separate descriptor tables, so the fixture opens the file through msvcrt there, in the same way as #5546.

Testing

The suite was tested against:

  • osgeo/grass-gis:main-ubuntu
  • osgeo/grass-gis:main-alpine

All 95 tests pass in both images:

  • with -W error
  • with GISBASE unset
  • in random order with pytest-randomly
  • under pytest-xdist together with the lib/cluster, lib/rowio and lib/arraystats suites (164 passed)

I could not run the Windows-specific fixture code locally, so I am relying on the OSGeo4W job for that part.

As a regression check, I rebuilt lib/segment with 50 single-line mutations, one at a time, and ran the suite against each build. 45 were caught in both images. The remaining 5 do not change behavior or are not reachable through the public API.

Formatting and linting were checked with the pinned ruff version from .pre-commit-config.yaml (v0.17.3).

Not covered

  • Out-of-range rows and columns in Segment_get() and Segment_put(). There is no bounds checking, so these read or write memory the segment does not own.
  • Failed seeks while paging, which end in G_fatal_error().
  • What Segment_get_row() returns without a preceding Segment_flush(), since that is not documented.
  • Segment_release() on an in-memory segment (see below).

Separate issues found

While writing the tests, I found two issues in the library. I am keeping them out of this PR and will open separate issues for them.

  1. Segment_open() computes the number of segments before it validates its parameters. A zero srows or scols divides by zero (SIGFPE). A zero nrows or ncols, and some negative srows or scols values, return 1 instead of the documented -3. A negative nrows or ncols ends in G_fatal_error() from an oversized allocation.
  2. Segment_release() crashes (SIGSEGV) on a segment that Segment_open() keeps entirely in memory. In that mode Segment_open() leaves the control block array NULL, but Segment_release() still loops over it. Segment_close() handles this case, but Segment_release() is public and is also called directly by grass.pygrass.raster.segment.Segment.release().

Both reproduce on Alpine and Ubuntu.

I also found that the comments in the lib/cluster and lib/arraystats tests (#7919, #7879) attribute the silent exit from G_warning() to glibc versus musl. It actually depends on whether GISBASE is set: the Alpine image sets it and the Ubuntu image does not. I plan to correct those comments, and move the shared fixtures into a common conftest.py, in a follow-up PR.

AI Disclosure

I used AI assistance (Claude) while drafting the tests and during verification, including the cross-platform runs, the mutation testing, and reproducing the two library issues.

I reviewed the tests and verification results myself and made the final decisions about the coverage, expected behavior, and scope of the changes.

@jalpatel11
jalpatel11 force-pushed the add-lib-segment-ctypes-tests branch from f77281a to 166820d Compare December 4, 2026 21:52
@jalpatel11
jalpatel11 marked this pull request as ready for review December 4, 2026 21:53
@jalpatel11

Copy link
Copy Markdown
Contributor Author

@echoix Hey buddy, I am back with the contribution. Sorry had a lot of work in the office. This is ready for review.

@github-actions github-actions Bot added Python Related code is in Python libraries tests Related to Test Suite labels Dec 4, 2026

@wenzeslaus wenzeslaus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The segment test description would be more valuable as real doc rather than lengthy test description. But that can be a separate PR.

Consider moving the the "no init" fixture higher to avoid duplication.

The statements in the description and the doc need verification (there is sth around the "no init" which is wrong).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries Python Related code is in Python tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants