Repository navigation
lib/segment: Add pytest ctypes test suite - #8316
Open
jalpatel11 wants to merge 1 commit into
Open
jalpatel11 wants to merge 1 commit into
jalpatel11 wants to merge 1 commit into
Conversation
jalpatel11
force-pushed
the
add-lib-segment-ctypes-tests
branch
from
December 4, 2026 21:52
f77281a to
166820d
Compare
jalpatel11
marked this pull request as ready for review
December 4, 2026 21:53
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. |
wenzeslaus
requested changes
Dec 5, 2026
wenzeslaus
left a comment
Member
There was a problem hiding this comment.
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
lib/segmentis 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 byr.cost,r.walk,r.watershed(segmented mode),r.stream.extract,i.segment,v.surf.bsplineand others, but it currently has no tests of any kind. This PR adds a pytest suite that calls the C API directly through the existinggrass.lib.segmentctypes 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.pyfor the fixtures.Each test checks behavior documented in the doc comments of
lib/segment/*.cor insegmentlib.dox. The one exception is a check that the parametrized layouts really exercise the code paths they are named for.lib_segment_ctypes_test.pyRandom access through
Segment_open()Segment_put()andSegment_get()round trip for every cellnseg = 1)Segment_put_row()andSegment_get_row()round trip for whole rowsSegment_flush()before the firstSegment_get_row(), and its documented return value of 0These tests run in both modes of
Segment_open(): paging to a file, and keeping the whole matrix in memory whennsegcovers every segment.They are also parametrized over 7 matrix and segment shapes.
setup.cuses a shift-and-mask address calculation whensrowsandscolsare 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 untouchedSegment_open()returning -3 for an illegal value length, and removing the file it createdSegment_close()deleting the segment file, and returning -1 for a segment that is not openLow-level API
Segment_format()zero-filling the file, thenSegment_init(), put/get andSegment_release(). The file is filled with0xfffirst, so the zero check is meaningful.Segment_format()andSegment_format_nofill()returning -3 for each non-positive parameterSegment_init()returning -1 for a file that is empty or shorter than a headerSegment_release()returning -1 for a segment that is not openconftest.pyThe segment functions call
G_debug(), andG_warning()on some failure paths, but never touch a project or mapset. Instead of creating a GRASS session, the fixtures:g.messageandg.dirsepsdo. ctypesgen does not translate theG_no_gisinit()macro, so the fixture callsG__no_gisinit(GIS_H_VERSION). On Windows this also makes files open in binary mode, whichSegment_open()needs.G_set_error_routine(), followinggui/wxpython/vdigit/wxdisplay.py, so thatG_warning()does not exit the process whenGISBASEis 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
msvcrtthere, in the same way as #5546.Testing
The suite was tested against:
osgeo/grass-gis:main-ubuntuosgeo/grass-gis:main-alpineAll 95 tests pass in both images:
-W errorGISBASEunsetpytest-randomlypytest-xdisttogether with thelib/cluster,lib/rowioandlib/arraystatssuites (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/segmentwith 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
ruffversion from.pre-commit-config.yaml(v0.17.3).Not covered
Segment_get()andSegment_put(). There is no bounds checking, so these read or write memory the segment does not own.G_fatal_error().Segment_get_row()returns without a precedingSegment_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.
Segment_open()computes the number of segments before it validates its parameters. A zerosrowsorscolsdivides by zero (SIGFPE). A zeronrowsorncols, and some negativesrowsorscolsvalues, return 1 instead of the documented -3. A negativenrowsorncolsends inG_fatal_error()from an oversized allocation.Segment_release()crashes (SIGSEGV) on a segment thatSegment_open()keeps entirely in memory. In that modeSegment_open()leaves the control block array NULL, butSegment_release()still loops over it.Segment_close()handles this case, butSegment_release()is public and is also called directly bygrass.pygrass.raster.segment.Segment.release().Both reproduce on Alpine and Ubuntu.
I also found that the comments in the
lib/clusterandlib/arraystatstests (#7919, #7879) attribute the silent exit fromG_warning()to glibc versus musl. It actually depends on whetherGISBASEis 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 commonconftest.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.