Permalink
Browse files

Fix mmap_window__in_region() to take ->mmap_pos into account

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
  • Loading branch information...
1 parent b1392a4 commit 7a2e1ec1225642c9fc6cc925542a2e367aaedf12 Pekka Enberg committed Nov 29, 2009
Showing with 93 additions and 7 deletions.
  1. +4 −2 Makefile
  2. +10 −2 harness.c
  3. +6 −1 include/pstore/mmap-window.h
  4. +5 −1 include/pstore/test/harness.h
  5. +1 −1 mmap-window.c
  6. +67 −0 test/mmap-window-test.c
View
@@ -100,7 +100,9 @@ TEST_RUNNER_C := test/test-runner.c
TEST_OBJS := csv.o
TEST_OBJS += die.o
TEST_OBJS += harness.o
+TEST_OBJS += mmap-window.o
TEST_OBJS += test/csv-test.o
+TEST_OBJS += test/mmap-window-test.o
TEST_OBJS += test/test-runner.o
TEST_DEPS := $(patsubst %.o,%.d,$(TEST_OBJS))
@@ -144,11 +146,11 @@ test: $(TEST_PROGRAM)
$(TEST_RUNNER_C): $(FORCE)
$(E) " GEN " $@
- $(Q) sh scripts/gen-test-runner test/*-test.c > $@
+ $(Q) sh scripts/gen-test-runner "test/*-test.c" > $@
$(TEST_SUITE_H): $(FORCE)
$(E) " GEN " $@
- $(Q) sh scripts/gen-test-proto test/*-test.c > $@
+ $(Q) sh scripts/gen-test-proto "test/*-test.c" > $@
$(TEST_PROGRAM): $(TEST_SUITE_H) $(TEST_DEPS) $(TEST_OBJS)
$(E) " LINK " $@
View
@@ -12,10 +12,18 @@ void assert_is_null(const void *p)
}
}
-void assert_false(bool value)
+void do_assert_true(const char *filename, int line, bool value)
+{
+ if (value == false) {
+ printf("%s:%d: Expected: true, but was false\n", filename, line);
+ exit(EXIT_FAILURE);
+ }
+}
+
+void do_assert_false(const char *filename, int line, bool value)
{
if (value == true) {
- printf("Expected: false, but was true\n");
+ printf("%s:%d: Expected: false, but was true\n", filename, line);
exit(EXIT_FAILURE);
}
}
@@ -32,9 +32,14 @@ static inline bool mmap_window__in_window(struct mmap_window *self, void *p)
return (ssize_t)(p - self->mmap_end) < 0;
}
+static inline size_t mmap_window__ptr_pos(struct mmap_window *self, void *p)
+{
+ return p - self->mmap - self->mmap_pos;
+}
+
static inline bool mmap_window__in_region(struct mmap_window *self, void *p)
{
- size_t mmap_pos = p - self->mmap;
+ size_t mmap_pos = mmap_window__ptr_pos(self, p);
return (int64_t)((uint64_t)(self->pos + mmap_pos) - self->length) < 0;
}
@@ -4,9 +4,13 @@
#include <stdbool.h>
#include <stddef.h>
+#define assert_true(value) do_assert_true(__FILE__, __LINE__, value)
+#define assert_false(value) do_assert_false(__FILE__, __LINE__, value)
+
void assert_is_null(const void *p);
void assert_int_equals(int expected, int actual);
void assert_str_equals(const char *s1, const char *s2, size_t len);
-void assert_false(bool value);
+void do_assert_true(const char *filename, int line, bool value);
+void do_assert_false(const char *filename, int line, bool value);
#endif /* PSTORE_HARNESS_H */
View
@@ -87,7 +87,7 @@ void *mmap_window__slide(struct mmap_window *self, void *p)
size_t mmap_pos;
uint64_t pos;
- mmap_pos = p - self->mmap - self->mmap_pos;
+ mmap_pos = mmap_window__ptr_pos(self, p);
pos = self->pos + mmap_pos;
View
@@ -0,0 +1,67 @@
+#include "pstore/test/harness.h"
+#include "pstore/mmap-window.h"
+#include "test-suite.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#define MMAP_START ((void *) 0x1000UL)
+#define MMAP_POS 0x100UL
+#define MMAP_END ((void *) 0x2000UL)
+#define MMAP_WINDOW_LEN (MMAP_END - MMAP_START - MMAP_POS)
+
+#define REGION_LEN 0x4000UL
+
+static struct mmap_window mmap;
+
+static void setup(void)
+{
+ mmap = (struct mmap_window) {
+ .mmap = MMAP_START,
+ .mmap_pos = MMAP_POS,
+ .mmap_end = MMAP_END,
+
+ .length = REGION_LEN,
+ };
+}
+
+static void teardown(void)
+{
+}
+
+void test_mmap_window_in_window(void)
+{
+ setup();
+
+ assert_true(mmap_window__in_window(&mmap, mmap_window__start(&mmap) + MMAP_WINDOW_LEN - 1));
+
+ teardown();
+}
+
+void test_mmap_window_not_in_window(void)
+{
+ setup();
+
+ assert_false(mmap_window__in_window(&mmap, mmap_window__start(&mmap) + MMAP_WINDOW_LEN));
+
+ teardown();
+}
+
+void test_mmap_window_in_region(void)
+{
+ setup();
+
+ assert_true(mmap_window__in_region(&mmap, mmap_window__start(&mmap) + MMAP_WINDOW_LEN));
+ assert_true(mmap_window__in_region(&mmap, mmap_window__start(&mmap) + REGION_LEN - 1));
+
+ teardown();
+}
+
+void test_mmap_window_not_in_region(void)
+{
+ setup();
+
+ assert_false(mmap_window__in_region(&mmap, mmap_window__start(&mmap) + REGION_LEN));
+
+ teardown();
+}

0 comments on commit 7a2e1ec

Please sign in to comment.