Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Clang sanitizer checks in CI #711

Merged
merged 5 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 61 additions & 6 deletions .github/workflows/test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,70 @@ on:
branches: [master, stable-*]

jobs:
tests:
name: Test Compile ${{ matrix.compiler }}
gcc-tests:
name: Test Compile gcc
runs-on: ubuntu-latest
env:
CC: ${{ matrix.compiler }}
CC: gcc

steps:
- uses: actions/[email protected]

- name: Install Doxygen
run: |
sudo apt update
sudo apt-get install doxygen graphviz clang-format-9

- name: Configure build
run: cmake -Bbuild -DWARNINGS_AS_ERRORS=ON .

- name: Formatting check
working-directory: build
run: |
clang-format-9 --version
make format
git diff --exit-code

- name: Build
working-directory: build
run: make

- name: binding-functions
working-directory: build
run: |
make binding-functions
test -s binding-functions

- name: Tests
working-directory: build
run: |
make test
sudo make install

# Note the packages aren't used to test the examples below
- name: Test packaging
working-directory: build
run: cpack -D CPACK_PACKAGE_CONTACT="Test build in CI"

- name: Examples
run: |
mkdir build/examples
cd build/examples
cmake ../../examples
make
make test

clang-tests:
name: Test Compile clang ${{ matrix.compile_opt }}
runs-on: ubuntu-latest
env:
CC: clang

strategy:
matrix:
compiler: [clang, gcc]
# See Clang docs for more information on the sanitizers:
# https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
compile_opt: ["", "-fsanitize=undefined,float-divide-by-zero -fno-sanitize-recover=undefined,float-divide-by-zero", "-fsanitize=memory -fno-sanitize-recover=memory", "-fsanitize=address -fno-sanitize-recover=address"]

steps:
- uses: actions/[email protected]
Expand All @@ -26,7 +81,7 @@ jobs:
sudo apt-get install doxygen graphviz clang-format-9

- name: Configure build
run: cmake -Bbuild -DWARNINGS_AS_ERRORS=ON .
run: cmake -Bbuild -DWARNINGS_AS_ERRORS=ON -DCMAKE_C_FLAGS="${{ matrix.compile_opt }}" .

- name: Formatting check
working-directory: build
Expand Down Expand Up @@ -60,7 +115,7 @@ jobs:
run: |
mkdir build/examples
cd build/examples
cmake ../../examples
cmake -DCMAKE_C_FLAGS="${{ matrix.compile_opt }}" ../../examples
make
make test

Expand Down
47 changes: 31 additions & 16 deletions src/apps/testapps/testPolygonToCells.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,27 @@ static GeoLoop holeGeoLoop = {.numVerts = 3, .verts = holeVerts};
static GeoPolygon holeGeoPolygon;

static LatLng emptyVerts[] = {{0.659966917655, -2.1364398519394},
{0.659966917655, -2.1364398519395},
{0.659966917655, -2.1364398519396}};
{0.659966917656, -2.1364398519395},
{0.659966917657, -2.1364398519396}};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did these need to be changed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Possibly they didn't need to - I was trying to adjust some test cases to get better coverage although I don't think I really accomplished that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My mistake, they do need to be changed because as defined this is a straight line. polygonToCells will reject straight lines now - even if a cell centroid is along the line - because it would have attempted to divide by zero when estimating.

static GeoLoop emptyGeoLoop = {.numVerts = 3, .verts = emptyVerts};
static GeoPolygon emptyGeoPolygon;

static LatLng invalidVerts[] = {{INFINITY, INFINITY}, {-INFINITY, -INFINITY}};
static GeoLoop invalidGeoLoop = {.numVerts = 2, .verts = invalidVerts};
static GeoPolygon invalidGeoPolygon;

static LatLng invalid2Verts[] = {{NAN, NAN}, {-NAN, -NAN}};
static GeoLoop invalid2GeoLoop = {.numVerts = 2, .verts = invalid2Verts};
static GeoPolygon invalid2GeoPolygon;

static LatLng pointVerts[] = {{0, 0}};
static GeoLoop pointGeoLoop = {.numVerts = 1, .verts = pointVerts};
static GeoPolygon pointGeoPolygon;

static LatLng lineVerts[] = {{0, 0}, {1, 0}};
static GeoLoop lineGeoLoop = {.numVerts = 2, .verts = lineVerts};
static GeoPolygon lineGeoPolygon;

/**
* Return true if the cell crosses the meridian.
*/
Expand Down Expand Up @@ -141,9 +149,15 @@ SUITE(polygonToCells) {
invalidGeoPolygon.geoloop = invalidGeoLoop;
invalidGeoPolygon.numHoles = 0;

invalid2GeoPolygon.geoloop = invalid2GeoLoop;
invalid2GeoPolygon.numHoles = 0;

pointGeoPolygon.geoloop = pointGeoLoop;
pointGeoPolygon.numHoles = 0;

lineGeoPolygon.geoloop = lineGeoLoop;
lineGeoPolygon.numHoles = 0;

TEST(maxPolygonToCellsSize) {
int64_t numHexagons;
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(&sfGeoPolygon, 9, 0,
Expand Down Expand Up @@ -457,9 +471,13 @@ SUITE(polygonToCells) {

TEST(polygonToCellsInvalid) {
int64_t numHexagons;
t_assert(H3_EXPORT(maxPolygonToCellsSize)(&invalidGeoPolygon, 9, 0,
t_assert(
H3_EXPORT(maxPolygonToCellsSize)(&invalidGeoPolygon, 9, 0,
&numHexagons) == E_FAILED,
"Cannot determine cell size to invalid geo polygon with Infinity");
t_assert(H3_EXPORT(maxPolygonToCellsSize)(&invalid2GeoPolygon, 9, 0,
&numHexagons) == E_FAILED,
"Cannot determine cell size to invalid geo polygon");
"Cannot determine cell size to invalid geo polygon with NaNs");

// Chosen arbitrarily, polygonToCells should error out before this is an
// issue.
Expand All @@ -474,18 +492,15 @@ SUITE(polygonToCells) {

TEST(polygonToCellsPoint) {
int64_t numHexagons;
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(&pointGeoPolygon, 9, 0,
&numHexagons));
// 0 estimation, plus 1 vertex, plus 12 buffer
t_assert(numHexagons == 13, "Point has expected 13 hexagons");
t_assert(H3_EXPORT(maxPolygonToCellsSize)(&pointGeoPolygon, 9, 0,
&numHexagons) == E_FAILED,
"Cannot estimate for single point");
}

H3Index *hexagons = calloc(numHexagons, sizeof(H3Index));
t_assertSuccess(
H3_EXPORT(polygonToCells)(&pointGeoPolygon, 9, 0, hexagons));
for (int i = 0; i < numHexagons; i++) {
t_assert(hexagons[i] == H3_NULL,
"no results for polygonToCells of a single point");
}
free(hexagons);
TEST(polygonToCellsLine) {
int64_t numHexagons;
t_assert(H3_EXPORT(maxPolygonToCellsSize)(&lineGeoPolygon, 9, 0,
&numHexagons) == E_FAILED,
"Cannot estimate for straight line");
}
}
7 changes: 6 additions & 1 deletion src/h3lib/lib/bbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ H3Error bboxHexEstimate(const BBox *bbox, int res, int64_t *out) {
double d = H3_EXPORT(greatCircleDistanceKm)(&p1, &p2);
// Derived constant based on: https://math.stackexchange.com/a/1921940
// Clamped to 3 as higher values tend to rapidly drag the estimate to zero.
double a = d * d / fmin(3.0, fabs((p1.lng - p2.lng) / (p1.lat - p2.lat)));
double lngDiff = p1.lng - p2.lng;
double latDiff = p1.lat - p2.lat;
if (lngDiff == 0 || latDiff == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

For my info, how are NAN and Infinity values handled here? Do we need to check for something like isFinite as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

isfinite is checked below

return E_FAILED;
}
double a = d * d / fmin(3.0, fabs(lngDiff / latDiff));

// Divide the two to get an estimate of the number of hexagons needed
double estimateDouble = ceil(a / pentagonAreaKm2);
Expand Down