h3 3.5.0 → 3.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +45 -0
  3. data/Gemfile.lock +9 -7
  4. data/README.md +8 -8
  5. data/ext/h3/src/.github/workflows/test-linux.yml +118 -0
  6. data/ext/h3/src/.github/workflows/test-macos.yml +42 -0
  7. data/ext/h3/src/.github/workflows/test-website.yml +32 -0
  8. data/ext/h3/src/.github/workflows/test-windows.yml +44 -0
  9. data/ext/h3/src/.gitignore +5 -0
  10. data/ext/h3/src/.travis.yml +21 -32
  11. data/ext/h3/src/CHANGELOG.md +60 -0
  12. data/ext/h3/src/CMakeLists.txt +150 -33
  13. data/ext/h3/src/CONTRIBUTING.md +1 -1
  14. data/ext/h3/src/README.md +65 -16
  15. data/ext/h3/src/RELEASE.md +3 -1
  16. data/ext/h3/src/VERSION +1 -1
  17. data/ext/h3/src/dev-docs/RFCs/rfc-template.md +21 -0
  18. data/ext/h3/src/dev-docs/RFCs/v4.0.0/error-handling-rfc.md +21 -0
  19. data/ext/h3/src/dev-docs/RFCs/v4.0.0/names_for_concepts_types_functions.md +276 -0
  20. data/ext/h3/src/dev-docs/RFCs/v4.0.0/overrideable-allocators-rfc.md +141 -0
  21. data/ext/h3/src/dev-docs/RFCs/v4.0.0/polyfill-modes-rfc.md +21 -0
  22. data/ext/h3/src/dev-docs/RFCs/v4.0.0/vertex-mode-rfc.md +50 -0
  23. data/ext/h3/src/dev-docs/build_windows.md +6 -1
  24. data/ext/h3/src/dev-docs/creating_bindings.md +3 -3
  25. data/ext/h3/src/dev-docs/custom_alloc.md +27 -0
  26. data/ext/h3/src/docs/{overview/mainpage.md → README.md} +2 -3
  27. data/ext/h3/src/docs/api/hierarchy.md +8 -0
  28. data/ext/h3/src/docs/api/misc.md +94 -0
  29. data/ext/h3/src/docs/community/applications.md +1 -0
  30. data/ext/h3/src/docs/community/bindings.md +10 -0
  31. data/ext/h3/src/docs/community/tutorials.md +8 -3
  32. data/ext/h3/src/docs/core-library/coordsystems.md +5 -4
  33. data/ext/h3/src/docs/core-library/filters.md +8 -9
  34. data/ext/h3/src/docs/core-library/geoToH3desc.md +2 -3
  35. data/ext/h3/src/docs/core-library/h3ToGeoBoundaryDesc.md +4 -5
  36. data/ext/h3/src/docs/core-library/h3ToGeoDesc.md +3 -4
  37. data/ext/h3/src/docs/core-library/h3indexing.md +26 -17
  38. data/ext/h3/src/docs/core-library/overview.md +2 -3
  39. data/ext/h3/src/docs/core-library/restable.md +1 -2
  40. data/ext/h3/src/docs/core-library/usage.md +1 -2
  41. data/ext/h3/src/docs/table-of-contents.json +47 -0
  42. data/ext/h3/src/docs/{overview/usecases.md → usecases.md} +6 -11
  43. data/ext/h3/src/scripts/binding_functions.sh +1 -1
  44. data/ext/h3/src/scripts/coverage.sh.in +8 -4
  45. data/ext/h3/src/scripts/update_version.sh +2 -2
  46. data/ext/h3/src/src/apps/applib/include/args.h +1 -0
  47. data/ext/h3/src/src/apps/applib/include/test.h +1 -0
  48. data/ext/h3/src/src/apps/applib/include/utility.h +7 -1
  49. data/ext/h3/src/src/apps/applib/lib/args.c +2 -0
  50. data/ext/h3/src/src/apps/applib/lib/kml.c +2 -0
  51. data/ext/h3/src/src/apps/applib/lib/test.c +1 -0
  52. data/ext/h3/src/src/apps/applib/lib/utility.c +133 -2
  53. data/ext/h3/src/src/apps/benchmarks/benchmarkH3Api.c +1 -1
  54. data/ext/h3/src/{website/html.config.js → src/apps/benchmarks/benchmarkH3UniEdge.c} +15 -12
  55. data/ext/h3/src/src/apps/filters/h3ToComponents.c +1 -0
  56. data/ext/h3/src/src/apps/filters/h3ToGeo.c +1 -0
  57. data/ext/h3/src/src/apps/filters/h3ToGeoBoundary.c +1 -0
  58. data/ext/h3/src/src/apps/filters/h3ToLocalIj.c +1 -0
  59. data/ext/h3/src/src/apps/filters/hexRange.c +1 -0
  60. data/ext/h3/src/src/apps/filters/kRing.c +1 -0
  61. data/ext/h3/src/src/apps/filters/localIjToH3.c +1 -0
  62. data/ext/h3/src/src/apps/miscapps/generateBaseCellNeighbors.c +2 -2
  63. data/ext/h3/src/src/apps/miscapps/generateFaceCenterPoint.c +1 -0
  64. data/ext/h3/src/src/apps/miscapps/generateNumHexagons.c +1 -2
  65. data/ext/h3/src/src/apps/miscapps/generatePentagonDirectionFaces.c +67 -0
  66. data/ext/h3/src/src/apps/miscapps/h3ToGeoBoundaryHier.c +1 -0
  67. data/ext/h3/src/src/apps/miscapps/h3ToGeoHier.c +1 -0
  68. data/ext/h3/src/src/apps/miscapps/h3ToHier.c +1 -0
  69. data/ext/h3/src/src/apps/testapps/mkRandGeo.c +1 -0
  70. data/ext/h3/src/src/apps/testapps/mkRandGeoBoundary.c +1 -0
  71. data/ext/h3/src/src/apps/testapps/testBBox.c +1 -0
  72. data/ext/h3/src/src/apps/testapps/testBaseCells.c +15 -1
  73. data/ext/h3/src/src/apps/testapps/testCompact.c +121 -2
  74. data/ext/h3/src/src/apps/testapps/testCoordIj.c +1 -0
  75. data/ext/h3/src/src/apps/testapps/testGeoCoord.c +47 -8
  76. data/ext/h3/src/src/apps/testapps/testGeoToH3.c +1 -0
  77. data/ext/h3/src/src/apps/testapps/testH3Api.c +1 -0
  78. data/ext/h3/src/src/apps/testapps/testH3CellArea.c +47 -0
  79. data/ext/h3/src/src/apps/testapps/testH3CellAreaExhaustive.c +180 -0
  80. data/ext/h3/src/src/apps/testapps/testH3Distance.c +2 -50
  81. data/ext/h3/src/src/apps/testapps/testH3DistanceExhaustive.c +84 -0
  82. data/ext/h3/src/src/apps/testapps/testH3GetFaces.c +1 -0
  83. data/ext/h3/src/src/apps/testapps/testH3Index.c +33 -3
  84. data/ext/h3/src/src/apps/testapps/testH3Line.c +2 -84
  85. data/ext/h3/src/src/apps/testapps/testH3LineExhaustive.c +115 -0
  86. data/ext/h3/src/src/apps/testapps/testH3Memory.c +175 -0
  87. data/ext/h3/src/src/apps/testapps/testH3NeighborRotations.c +1 -0
  88. data/ext/h3/src/src/apps/testapps/testH3SetToLinkedGeo.c +1 -0
  89. data/ext/h3/src/src/apps/testapps/testH3SetToVertexGraph.c +1 -0
  90. data/ext/h3/src/src/apps/testapps/testH3ToCenterChild.c +68 -0
  91. data/ext/h3/src/src/apps/testapps/testH3ToChildren.c +15 -2
  92. data/ext/h3/src/src/apps/testapps/testH3ToGeo.c +1 -0
  93. data/ext/h3/src/src/apps/testapps/testH3ToGeoBoundary.c +1 -0
  94. data/ext/h3/src/src/apps/testapps/testH3ToLocalIj.c +24 -236
  95. data/ext/h3/src/src/apps/testapps/testH3ToLocalIjExhaustive.c +265 -0
  96. data/ext/h3/src/src/apps/testapps/testH3ToParent.c +1 -0
  97. data/ext/h3/src/src/apps/testapps/testH3UniEdge.c +45 -16
  98. data/ext/h3/src/src/apps/testapps/testH3UniEdgeExhaustive.c +111 -0
  99. data/ext/h3/src/src/apps/testapps/testHexRanges.c +1 -0
  100. data/ext/h3/src/src/apps/testapps/testHexRing.c +1 -0
  101. data/ext/h3/src/src/apps/testapps/testKRing.c +1 -0
  102. data/ext/h3/src/src/apps/testapps/testLinkedGeo.c +1 -0
  103. data/ext/h3/src/src/apps/testapps/testMaxH3ToChildrenSize.c +1 -0
  104. data/ext/h3/src/src/apps/testapps/testPentagonIndexes.c +58 -0
  105. data/ext/h3/src/src/apps/testapps/testPolyfill.c +72 -9
  106. data/ext/h3/src/src/apps/testapps/testPolyfillReported.c +157 -0
  107. data/ext/h3/src/src/apps/testapps/testPolygon.c +27 -1
  108. data/ext/h3/src/src/apps/testapps/testVec2d.c +1 -0
  109. data/ext/h3/src/src/apps/testapps/testVec3d.c +1 -0
  110. data/ext/h3/src/src/apps/testapps/testVertex.c +66 -0
  111. data/ext/h3/src/src/apps/testapps/testVertexGraph.c +1 -0
  112. data/ext/h3/src/src/h3lib/include/algos.h +8 -0
  113. data/ext/h3/src/src/h3lib/include/alloc.h +40 -0
  114. data/ext/h3/src/src/h3lib/include/baseCells.h +4 -0
  115. data/ext/h3/src/src/h3lib/include/bbox.h +4 -1
  116. data/ext/h3/src/src/h3lib/include/constants.h +2 -0
  117. data/ext/h3/src/src/h3lib/include/faceijk.h +3 -2
  118. data/ext/h3/src/src/h3lib/include/geoCoord.h +2 -3
  119. data/ext/h3/src/src/h3lib/include/h3Index.h +37 -4
  120. data/ext/h3/src/src/h3lib/include/h3api.h.in +85 -17
  121. data/ext/h3/src/src/h3lib/include/linkedGeo.h +1 -0
  122. data/ext/h3/src/src/h3lib/include/polygon.h +1 -0
  123. data/ext/h3/src/src/h3lib/include/polygonAlgos.h +1 -0
  124. data/ext/h3/src/src/h3lib/include/vertex.h +44 -0
  125. data/ext/h3/src/src/h3lib/include/vertexGraph.h +1 -0
  126. data/ext/h3/src/src/h3lib/lib/algos.c +305 -80
  127. data/ext/h3/src/src/h3lib/lib/baseCells.c +26 -4
  128. data/ext/h3/src/src/h3lib/lib/bbox.c +56 -27
  129. data/ext/h3/src/src/h3lib/lib/coordijk.c +2 -0
  130. data/ext/h3/src/src/h3lib/lib/faceijk.c +35 -24
  131. data/ext/h3/src/src/h3lib/lib/geoCoord.c +162 -44
  132. data/ext/h3/src/src/h3lib/lib/h3Index.c +150 -46
  133. data/ext/h3/src/src/h3lib/lib/h3UniEdge.c +42 -57
  134. data/ext/h3/src/src/h3lib/lib/linkedGeo.c +20 -15
  135. data/ext/h3/src/src/h3lib/lib/localij.c +5 -5
  136. data/ext/h3/src/src/h3lib/lib/polygon.c +3 -2
  137. data/ext/h3/src/src/h3lib/lib/vec2d.c +1 -0
  138. data/ext/h3/src/src/h3lib/lib/vec3d.c +1 -0
  139. data/ext/h3/src/src/h3lib/lib/vertex.c +134 -0
  140. data/ext/h3/src/src/h3lib/lib/vertexGraph.c +8 -5
  141. data/ext/h3/src/website/.eslintignore +2 -0
  142. data/ext/h3/src/website/.gitignore +57 -0
  143. data/ext/h3/src/website/.nvmrc +1 -0
  144. data/ext/h3/src/website/README.md +8 -6
  145. data/ext/h3/src/website/gatsby-config.js +83 -0
  146. data/ext/h3/src/website/package.json +20 -12
  147. data/ext/h3/src/website/scripts/build-to-gh-pages.sh +7 -5
  148. data/ext/h3/src/website/src/.gitkeep +0 -0
  149. data/ext/h3/src/website/templates/documentation.jsx +129 -0
  150. data/ext/h3/src/website/yarn.lock +13723 -0
  151. data/h3.gemspec +1 -0
  152. data/lib/h3.rb +8 -23
  153. data/lib/h3/bindings/base.rb +15 -4
  154. data/lib/h3/bindings/private.rb +13 -9
  155. data/lib/h3/geo_json.rb +1 -1
  156. data/lib/h3/hierarchy.rb +24 -9
  157. data/lib/h3/indexing.rb +7 -7
  158. data/lib/h3/inspection.rb +22 -26
  159. data/lib/h3/miscellaneous.rb +157 -9
  160. data/lib/h3/regions.rb +3 -0
  161. data/lib/h3/traversal.rb +9 -9
  162. data/lib/h3/unidirectional_edges.rb +18 -18
  163. data/lib/h3/version.rb +1 -1
  164. data/spec/geo_json_spec.rb +8 -0
  165. data/spec/hierarchy_spec.rb +23 -13
  166. data/spec/indexing_spec.rb +15 -15
  167. data/spec/inspection_spec.rb +17 -17
  168. data/spec/miscellaneous_spec.rb +151 -6
  169. data/spec/{region_spec.rb → regions_spec.rb} +1 -1
  170. data/spec/traversal_spec.rb +6 -6
  171. data/spec/unidirectional_edges_spec.rb +18 -18
  172. metadata +55 -15
  173. data/ext/h3/src/.ycm_extra_conf.py +0 -92
  174. data/ext/h3/src/appveyor.yml +0 -50
  175. data/ext/h3/src/website/src/config.js +0 -46
  176. data/ext/h3/src/website/src/mdRoutes.js +0 -151
  177. data/ext/h3/src/website/src/styles/_variables.scss +0 -16
  178. data/ext/h3/src/website/src/styles/index.scss +0 -3
  179. data/ext/h3/src/website/static/index.html +0 -15
  180. data/lib/h3/bindings.rb +0 -12
@@ -19,6 +19,7 @@
19
19
 
20
20
  #include <stdio.h>
21
21
  #include <stdlib.h>
22
+
22
23
  #include "baseCells.h"
23
24
  #include "h3Index.h"
24
25
  #include "h3api.h"
@@ -22,6 +22,7 @@
22
22
  #include <stdio.h>
23
23
  #include <stdlib.h>
24
24
  #include <string.h>
25
+
25
26
  #include "constants.h"
26
27
  #include "h3Index.h"
27
28
  #include "test.h"
@@ -109,12 +110,41 @@ SUITE(h3Index) {
109
110
  for (int i = 0; i <= 0xf; i++) {
110
111
  H3Index h = H3_INIT;
111
112
  H3_SET_MODE(h, i);
112
- char failureMessage[BUFF_SIZE];
113
- sprintf(failureMessage, "h3IsValid failed on mode %d", i);
114
- t_assert(!H3_EXPORT(h3IsValid)(h) || i == 1, failureMessage);
113
+ if (i == H3_HEXAGON_MODE) {
114
+ t_assert(H3_EXPORT(h3IsValid)(h),
115
+ "h3IsValid succeeds on valid mode");
116
+ } else {
117
+ char failureMessage[BUFF_SIZE];
118
+ sprintf(failureMessage, "h3IsValid failed on mode %d", i);
119
+ t_assert(!H3_EXPORT(h3IsValid)(h), failureMessage);
120
+ }
115
121
  }
116
122
  }
117
123
 
124
+ TEST(h3IsValidReservedBits) {
125
+ for (int i = 0; i < 8; i++) {
126
+ H3Index h = H3_INIT;
127
+ H3_SET_MODE(h, H3_HEXAGON_MODE);
128
+ H3_SET_RESERVED_BITS(h, i);
129
+ if (i == 0) {
130
+ t_assert(H3_EXPORT(h3IsValid)(h),
131
+ "h3IsValid succeeds on valid reserved bits");
132
+ } else {
133
+ char failureMessage[BUFF_SIZE];
134
+ sprintf(failureMessage, "h3IsValid failed on reserved bits %d",
135
+ i);
136
+ t_assert(!H3_EXPORT(h3IsValid)(h), failureMessage);
137
+ }
138
+ }
139
+ }
140
+
141
+ TEST(h3IsValidHighBit) {
142
+ H3Index h = H3_INIT;
143
+ H3_SET_MODE(h, H3_HEXAGON_MODE);
144
+ H3_SET_HIGH_BIT(h, 1);
145
+ t_assert(!H3_EXPORT(h3IsValid)(h), "h3IsValid failed on high bit");
146
+ }
147
+
118
148
  TEST(h3BadDigitInvalid) {
119
149
  H3Index h = H3_INIT;
120
150
  // By default the first index digit is out of range.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2018 Uber Technologies, Inc.
2
+ * Copyright 2018-2019 Uber Technologies, Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -22,96 +22,14 @@
22
22
  #include <stdio.h>
23
23
  #include <stdlib.h>
24
24
  #include <string.h>
25
+
25
26
  #include "h3Index.h"
26
27
  #include "h3api.h"
27
28
  #include "localij.h"
28
29
  #include "test.h"
29
30
  #include "utility.h"
30
31
 
31
- static const int MAX_DISTANCES[] = {1, 2, 5, 12, 19, 26};
32
-
33
- /**
34
- * Property-based testing of h3Line output
35
- */
36
- static void h3Line_assertions(H3Index start, H3Index end) {
37
- int sz = H3_EXPORT(h3LineSize)(start, end);
38
- t_assert(sz > 0, "got valid size");
39
- H3Index *line = calloc(sz, sizeof(H3Index));
40
-
41
- int err = H3_EXPORT(h3Line)(start, end, line);
42
-
43
- t_assert(err == 0, "no error on line");
44
- t_assert(line[0] == start, "line starts with start index");
45
- t_assert(line[sz - 1] == end, "line ends with end index");
46
-
47
- for (int i = 1; i < sz; i++) {
48
- t_assert(H3_EXPORT(h3IsValid)(line[i]), "index is valid");
49
- t_assert(H3_EXPORT(h3IndexesAreNeighbors)(line[i], line[i - 1]),
50
- "index is a neighbor of the previous index");
51
- if (i > 1) {
52
- t_assert(
53
- !H3_EXPORT(h3IndexesAreNeighbors)(line[i], line[i - 2]),
54
- "index is not a neighbor of the index before the previous");
55
- }
56
- }
57
-
58
- free(line);
59
- }
60
-
61
- /**
62
- * Tests for invalid h3Line input
63
- */
64
- static void h3Line_invalid_assertions(H3Index start, H3Index end) {
65
- int sz = H3_EXPORT(h3LineSize)(start, end);
66
- t_assert(sz < 0, "line size marked as invalid");
67
-
68
- H3Index *line = {0};
69
- int err = H3_EXPORT(h3Line)(start, end, line);
70
- t_assert(err != 0, "line marked as invalid");
71
- }
72
-
73
- /**
74
- * Test for lines from an index to all neighbors within a kRing
75
- */
76
- static void h3Line_kRing_assertions(H3Index h3) {
77
- int r = H3_GET_RESOLUTION(h3);
78
- t_assert(r <= 5, "resolution supported by test function (kRing)");
79
- int maxK = MAX_DISTANCES[r];
80
-
81
- int sz = H3_EXPORT(maxKringSize)(maxK);
82
-
83
- if (H3_EXPORT(h3IsPentagon)(h3)) {
84
- return;
85
- }
86
-
87
- H3Index *neighbors = calloc(sz, sizeof(H3Index));
88
- H3_EXPORT(kRing)(h3, maxK, neighbors);
89
-
90
- for (int i = 0; i < sz; i++) {
91
- if (neighbors[i] == 0) {
92
- continue;
93
- }
94
- int distance = H3_EXPORT(h3Distance)(h3, neighbors[i]);
95
- if (distance >= 0) {
96
- h3Line_assertions(h3, neighbors[i]);
97
- } else {
98
- h3Line_invalid_assertions(h3, neighbors[i]);
99
- }
100
- }
101
-
102
- free(neighbors);
103
- }
104
-
105
32
  SUITE(h3Line) {
106
- TEST(h3Line_kRing) {
107
- iterateAllIndexesAtRes(0, h3Line_kRing_assertions);
108
- iterateAllIndexesAtRes(1, h3Line_kRing_assertions);
109
- iterateAllIndexesAtRes(2, h3Line_kRing_assertions);
110
- // Don't iterate all of res 3, to save time
111
- iterateAllIndexesAtResPartial(3, h3Line_kRing_assertions, 6);
112
- // Further resolutions aren't tested to save time.
113
- }
114
-
115
33
  TEST(h3Line_acrossMultipleFaces) {
116
34
  H3Index start = 0x85285aa7fffffff;
117
35
  H3Index end = 0x851d9b1bfffffff;
@@ -0,0 +1,115 @@
1
+ /*
2
+ * Copyright 2019 Uber Technologies, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /** @file
17
+ * @brief tests H3 distance function using tests over a large number of indexes.
18
+ *
19
+ * usage: `testH3Distance`
20
+ */
21
+
22
+ #include <stdio.h>
23
+ #include <stdlib.h>
24
+ #include <string.h>
25
+
26
+ #include "h3Index.h"
27
+ #include "h3api.h"
28
+ #include "localij.h"
29
+ #include "test.h"
30
+ #include "utility.h"
31
+
32
+ static const int MAX_DISTANCES[] = {1, 2, 5, 12, 19, 26};
33
+
34
+ /**
35
+ * Property-based testing of h3Line output
36
+ */
37
+ static void h3Line_assertions(H3Index start, H3Index end) {
38
+ int sz = H3_EXPORT(h3LineSize)(start, end);
39
+ t_assert(sz > 0, "got valid size");
40
+ H3Index *line = calloc(sz, sizeof(H3Index));
41
+
42
+ int err = H3_EXPORT(h3Line)(start, end, line);
43
+
44
+ t_assert(err == 0, "no error on line");
45
+ t_assert(line[0] == start, "line starts with start index");
46
+ t_assert(line[sz - 1] == end, "line ends with end index");
47
+
48
+ for (int i = 1; i < sz; i++) {
49
+ t_assert(H3_EXPORT(h3IsValid)(line[i]), "index is valid");
50
+ t_assert(H3_EXPORT(h3IndexesAreNeighbors)(line[i], line[i - 1]),
51
+ "index is a neighbor of the previous index");
52
+ if (i > 1) {
53
+ t_assert(
54
+ !H3_EXPORT(h3IndexesAreNeighbors)(line[i], line[i - 2]),
55
+ "index is not a neighbor of the index before the previous");
56
+ }
57
+ }
58
+
59
+ free(line);
60
+ }
61
+
62
+ /**
63
+ * Tests for invalid h3Line input
64
+ */
65
+ static void h3Line_invalid_assertions(H3Index start, H3Index end) {
66
+ int sz = H3_EXPORT(h3LineSize)(start, end);
67
+ t_assert(sz < 0, "line size marked as invalid");
68
+
69
+ H3Index *line = {0};
70
+ int err = H3_EXPORT(h3Line)(start, end, line);
71
+ t_assert(err != 0, "line marked as invalid");
72
+ }
73
+
74
+ /**
75
+ * Test for lines from an index to all neighbors within a kRing
76
+ */
77
+ static void h3Line_kRing_assertions(H3Index h3) {
78
+ int r = H3_GET_RESOLUTION(h3);
79
+ t_assert(r <= 5, "resolution supported by test function (kRing)");
80
+ int maxK = MAX_DISTANCES[r];
81
+
82
+ int sz = H3_EXPORT(maxKringSize)(maxK);
83
+
84
+ if (H3_EXPORT(h3IsPentagon)(h3)) {
85
+ return;
86
+ }
87
+
88
+ H3Index *neighbors = calloc(sz, sizeof(H3Index));
89
+ H3_EXPORT(kRing)(h3, maxK, neighbors);
90
+
91
+ for (int i = 0; i < sz; i++) {
92
+ if (neighbors[i] == 0) {
93
+ continue;
94
+ }
95
+ int distance = H3_EXPORT(h3Distance)(h3, neighbors[i]);
96
+ if (distance >= 0) {
97
+ h3Line_assertions(h3, neighbors[i]);
98
+ } else {
99
+ h3Line_invalid_assertions(h3, neighbors[i]);
100
+ }
101
+ }
102
+
103
+ free(neighbors);
104
+ }
105
+
106
+ SUITE(h3Line) {
107
+ TEST(h3Line_kRing) {
108
+ iterateAllIndexesAtRes(0, h3Line_kRing_assertions);
109
+ iterateAllIndexesAtRes(1, h3Line_kRing_assertions);
110
+ iterateAllIndexesAtRes(2, h3Line_kRing_assertions);
111
+ // Don't iterate all of res 3, to save time
112
+ iterateAllIndexesAtResPartial(3, h3Line_kRing_assertions, 6);
113
+ // Further resolutions aren't tested to save time.
114
+ }
115
+ }
@@ -0,0 +1,175 @@
1
+ /*
2
+ * Copyright 2020 Uber Technologies, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /** @file
17
+ * @brief tests main H3 core library memory management.
18
+ *
19
+ * usage: `testH3Memory`
20
+ */
21
+
22
+ #include <math.h>
23
+ #include <string.h>
24
+
25
+ #include "geoCoord.h"
26
+ #include "h3Index.h"
27
+ #include "h3api.h"
28
+ #include "test.h"
29
+ #include "utility.h"
30
+
31
+ // Whether to fail all allocations
32
+ static bool failAlloc = false;
33
+ // Actual number of malloc/calloc/realloc calls observed
34
+ static int actualAllocCalls = 0;
35
+ // Actual number of free calls observed
36
+ static int actualFreeCalls = 0;
37
+ // Set to non-zero to begin failing allocations after a certain number of calls
38
+ static int permittedAllocCalls = 0;
39
+
40
+ void resetMemoryCounters(int permitted) {
41
+ failAlloc = false;
42
+ actualAllocCalls = 0;
43
+ actualFreeCalls = 0;
44
+ permittedAllocCalls = permitted;
45
+ }
46
+
47
+ void* test_prefix_malloc(size_t size) {
48
+ actualAllocCalls++;
49
+ if (permittedAllocCalls && actualAllocCalls > permittedAllocCalls) {
50
+ failAlloc = true;
51
+ }
52
+ if (failAlloc) {
53
+ return NULL;
54
+ }
55
+ return malloc(size);
56
+ }
57
+
58
+ void* test_prefix_calloc(size_t num, size_t size) {
59
+ actualAllocCalls++;
60
+ if (permittedAllocCalls && actualAllocCalls > permittedAllocCalls) {
61
+ failAlloc = true;
62
+ }
63
+ if (failAlloc) {
64
+ return NULL;
65
+ }
66
+ return calloc(num, size);
67
+ }
68
+
69
+ void* test_prefix_realloc(void* ptr, size_t size) {
70
+ actualAllocCalls++;
71
+ if (permittedAllocCalls && actualAllocCalls > permittedAllocCalls) {
72
+ failAlloc = true;
73
+ }
74
+ if (failAlloc) {
75
+ return NULL;
76
+ }
77
+ return realloc(ptr, size);
78
+ }
79
+
80
+ void test_prefix_free(void* ptr) {
81
+ actualFreeCalls++;
82
+ return free(ptr);
83
+ }
84
+
85
+ H3Index sunnyvale = 0x89283470c27ffff;
86
+ H3Index pentagon = 0x89080000003ffff;
87
+
88
+ SUITE(h3Memory) {
89
+ TEST(kRing) {
90
+ int k = 2;
91
+ int hexCount = H3_EXPORT(maxKringSize)(k);
92
+ H3Index* kRingOutput = calloc(hexCount, sizeof(H3Index));
93
+
94
+ resetMemoryCounters(0);
95
+ H3_EXPORT(kRing)(sunnyvale, k, kRingOutput);
96
+ t_assert(actualAllocCalls == 0, "kRing did not call alloc");
97
+ t_assert(actualFreeCalls == 0, "kRing did not call free");
98
+
99
+ resetMemoryCounters(0);
100
+ H3_EXPORT(kRing)(pentagon, k, kRingOutput);
101
+ t_assert(actualAllocCalls == 1, "kRing called alloc");
102
+ t_assert(actualFreeCalls == 1, "kRing called free");
103
+
104
+ resetMemoryCounters(0);
105
+ failAlloc = true;
106
+ memset(kRingOutput, 0, hexCount * sizeof(H3Index));
107
+ H3_EXPORT(kRing)(pentagon, k, kRingOutput);
108
+ t_assert(actualAllocCalls == 1, "kRing called alloc");
109
+ t_assert(actualFreeCalls == 0, "kRing did not call free");
110
+
111
+ for (int i = 0; i < hexCount; i++) {
112
+ t_assert(!kRingOutput[i],
113
+ "kRing did not produce output without alloc");
114
+ }
115
+
116
+ free(kRingOutput);
117
+ }
118
+
119
+ TEST(compact) {
120
+ int k = 9;
121
+ int hexCount = H3_EXPORT(maxKringSize)(k);
122
+ int expectedCompactCount = 73;
123
+
124
+ // Generate a set of hexagons to compact
125
+ H3Index* sunnyvaleExpanded = calloc(hexCount, sizeof(H3Index));
126
+ resetMemoryCounters(0);
127
+ H3_EXPORT(kRing)(sunnyvale, k, sunnyvaleExpanded);
128
+ t_assert(actualAllocCalls == 0, "kRing did not call alloc");
129
+ t_assert(actualFreeCalls == 0, "kRing did not call free");
130
+
131
+ H3Index* compressed = calloc(hexCount, sizeof(H3Index));
132
+
133
+ resetMemoryCounters(0);
134
+ failAlloc = true;
135
+ int err = H3_EXPORT(compact)(sunnyvaleExpanded, compressed, hexCount);
136
+ t_assert(err == COMPACT_ALLOC_FAILED, "malloc failed (1)");
137
+ t_assert(actualAllocCalls == 1, "alloc called once");
138
+ t_assert(actualFreeCalls == 0, "free not called");
139
+
140
+ resetMemoryCounters(1);
141
+ err = H3_EXPORT(compact)(sunnyvaleExpanded, compressed, hexCount);
142
+ t_assert(err == COMPACT_ALLOC_FAILED, "malloc failed (2)");
143
+ t_assert(actualAllocCalls == 2, "alloc called twice");
144
+ t_assert(actualFreeCalls == 1, "free called once");
145
+
146
+ resetMemoryCounters(2);
147
+ err = H3_EXPORT(compact)(sunnyvaleExpanded, compressed, hexCount);
148
+ t_assert(err == COMPACT_ALLOC_FAILED, "malloc failed (3)");
149
+ t_assert(actualAllocCalls == 3, "alloc called three times");
150
+ t_assert(actualFreeCalls == 2, "free called twice");
151
+
152
+ resetMemoryCounters(3);
153
+ err = H3_EXPORT(compact)(sunnyvaleExpanded, compressed, hexCount);
154
+ t_assert(err == COMPACT_ALLOC_FAILED, "compact failed (4)");
155
+ t_assert(actualAllocCalls == 4, "alloc called four times");
156
+ t_assert(actualFreeCalls == 3, "free called three times");
157
+
158
+ resetMemoryCounters(4);
159
+ err = H3_EXPORT(compact)(sunnyvaleExpanded, compressed, hexCount);
160
+ t_assert(err == COMPACT_SUCCESS, "compact using successful malloc");
161
+ t_assert(actualAllocCalls == 4, "alloc called four times");
162
+ t_assert(actualFreeCalls == 4, "free called four times");
163
+
164
+ int count = 0;
165
+ for (int i = 0; i < hexCount; i++) {
166
+ if (compressed[i] != 0) {
167
+ count++;
168
+ }
169
+ }
170
+ t_assert(count == expectedCompactCount, "got expected compacted count");
171
+
172
+ free(compressed);
173
+ free(sunnyvaleExpanded);
174
+ }
175
+ }