diff mbox series

[GSoC,4/6] t-reftable-pq: make merged_iter_pqueue_check() callable by reference

Message ID 20240606075601.6989-5-chandrapratap3519@gmail.com (mailing list archive)
State Superseded
Headers show
Series t: port reftable/pq_test.c to the unit testing | expand

Commit Message

Chandra Pratap June 6, 2024, 7:40 a.m. UTC
merged_iter_pqueue_check() checks the validity of a priority queue
represented by a merged_iter_pqueue struct by asserting the
parent-child relation in the struct's heap. Explicity passing a
struct to this function means a copy of the entire struct is created,
which is inefficient.

Make the function accept a pointer to the struct instead. This is
safe to do since the function doesn't modify the struct in any way.
Make the function parameter 'const' to assert immutability.

Mentored-by: Patrick Steinhardt <ps@pks.im>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
---
 t/unit-tests/t-reftable-pq.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Comments

Patrick Steinhardt June 6, 2024, 11:48 a.m. UTC | #1
On Thu, Jun 06, 2024 at 01:10:48PM +0530, Chandra Pratap wrote:
> diff --git a/t/unit-tests/t-reftable-pq.c b/t/unit-tests/t-reftable-pq.c
> index 5c5a4ecdc5..30bf9cb492 100644
> --- a/t/unit-tests/t-reftable-pq.c
> +++ b/t/unit-tests/t-reftable-pq.c
> @@ -10,11 +10,11 @@ license that can be found in the LICENSE file or at
>  #include "reftable/constants.h"
>  #include "reftable/pq.h"
>  
> -static void merged_iter_pqueue_check(struct merged_iter_pqueue pq)
> +static void merged_iter_pqueue_check(const struct merged_iter_pqueue *pq)
>  {
> -	for (int i = 1; i < pq.len; i++) {
> +	for (int i = 1; i < pq->len; i++) {

Nit: we might also change this to `size_t i` while at it. I know that
these changes are not super popular, but I eventually want Git to
compile cleanly with `-Wsign-conversion`. So the more issues we fix
while at it, the smaller the patch series for this needs to be.

Patrick
diff mbox series

Patch

diff --git a/t/unit-tests/t-reftable-pq.c b/t/unit-tests/t-reftable-pq.c
index 5c5a4ecdc5..30bf9cb492 100644
--- a/t/unit-tests/t-reftable-pq.c
+++ b/t/unit-tests/t-reftable-pq.c
@@ -10,11 +10,11 @@  license that can be found in the LICENSE file or at
 #include "reftable/constants.h"
 #include "reftable/pq.h"
 
-static void merged_iter_pqueue_check(struct merged_iter_pqueue pq)
+static void merged_iter_pqueue_check(const struct merged_iter_pqueue *pq)
 {
-	for (int i = 1; i < pq.len; i++) {
+	for (int i = 1; i < pq->len; i++) {
 		int parent = (i - 1) / 2;
-		check(pq_less(&pq.heap[parent], &pq.heap[i]));
+		check(pq_less(&pq->heap[parent], &pq->heap[i]));
 	}
 }
 
@@ -40,13 +40,13 @@  static void test_pq(void)
 		};
 
 		merged_iter_pqueue_add(&pq, &e);
-		merged_iter_pqueue_check(pq);
+		merged_iter_pqueue_check(&pq);
 		i = (i * 7) % N;
 	} while (i != 1);
 
 	while (!merged_iter_pqueue_is_empty(pq)) {
 		struct pq_entry e = merged_iter_pqueue_remove(&pq);
-		merged_iter_pqueue_check(pq);
+		merged_iter_pqueue_check(&pq);
 
 		check(reftable_record_type(e.rec) == BLOCK_TYPE_REF);
 		if (last)