Skip to content

Commit

Permalink
Merge vis-tests into test directory
Browse files Browse the repository at this point in the history
Going forward all tests should be submitted here directly.
  • Loading branch information
rnpnr committed May 22, 2024
2 parents b7f8018 + efafa3c commit b707402
Show file tree
Hide file tree
Showing 454 changed files with 114,700 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.out
*.err
*.status
*.busted
13 changes: 13 additions & 0 deletions test/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright © 2014-2018 Marc André Tanner, et al.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 changes: 16 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test:
@$(MAKE) -C core
@$(MAKE) -C lua
@$(MAKE) -C vis
@$(MAKE) -C sam
@$(MAKE) -C vim

clean:
@$(MAKE) -C core clean
@$(MAKE) -C lua clean
@$(MAKE) -C vis clean
@$(MAKE) -C sam clean
@$(MAKE) -C vim clean
@$(MAKE) -C util clean

.PHONY: test clean
54 changes: 54 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Testing Infrastructure for Vis
------------------------------

This repository contains testing infrastructure for the
[vis editor](https://github.com/martanne/vis). It is expected
to be cloned into a sub directory of the `vis` source tree.

There exist 5 different kinds of tests:

* `core` are C unit tests for core data structures used by vis
* `fuzz` infrastructure for automated fuzzing
* `vim` tests vim compatibility
* `sam` tests sam compatibility of the command language
* `vis` contains tests for vis specific behavior/features
* `lua` contains tests for the vis specific lua api

Run `make` to execute all test suites.

Writing good tests
------------------

Each sub directory contains a README with further information
about the specific test method.

Coming up with good and exhaustive tests is often non-trivial,
below are some recommendations:

* Make sure you understand what the expected behavior you
want to test is. Think about possible error conditions.

* Test something specific, but keep the overall context in mind.

For vi(m) frontend related tests consider behavior when given
a count or when the command is repeated (using `.`).

For example the motions `f`, `F`, `t`, `T` also influence `;` and `,`.
Similar, `*` and `#` affect `n` and `N`.

* Test special cases, these often reveal interesting behavior.

Continuing the motion example these might be: empty lines, single
letter words, no matches, consecutive matches, over-specified counts,
etc.

* Test systematically and strive for high test coverage.

It is preferable to have a small set of tests which cover a specific
functionality exhaustively, rather than lots of half-baked tests which
only test the basics.

A good first indication of the completeness of your tests is the
[achieved code coverage](https://codecov.io/gh/martanne/vis). Ideally
a test should primarily exercise a small set of functions which should
achieve high path coverage.
10 changes: 10 additions & 0 deletions test/core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/config.h
/text-test
/buffer-test
/map-test
/array-test
/ccan-config
*.gcda
*.gcno
*.gcov
*.valgrind
66 changes: 66 additions & 0 deletions test/core/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
-include ../../config.mk

ALL = buffer-test map-test array-test text-test
SRC = $(wildcard ccan/*/*.c)
CFLAGS += -I. -I../.. -DBUFFER_SIZE=4 -DBLOCK_SIZE=4

test: $(ALL)
@./buffer-test
@./map-test
@./array-test
@./text-test

config.h:
@echo Generating ccan configuration header
@${CC} ccan-config.c -o ccan-config && ./ccan-config "${CC}" ${CFLAGS} > config.h

text-test: config.h text-test.c ../../text.c ../../text-common.c ../../text-io.c ../../text-iterator.c ../../text-util.c ../../text-motions.c ../../text-objects.c ../../text-regex.c ../../array.c
@echo Compiling $@ binary
@${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} ${filter %.c, $^} ${SRC} ${LDFLAGS} -o $@

buffer-test: config.h buffer-test.c ../../buffer.c
@echo Compiling $@ binary
@${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} ${filter %.c, $^} ${SRC} ${LDFLAGS} -o $@

map-test: config.h map-test.c ../../map.c
@echo Compiling $@ binary
@${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} ${filter %.c, $^} ${SRC} ${LDFLAGS} -o $@

array-test: config.h array-test.c ../../array.c
@echo Compiling $@ binary
@${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} ${filter %.c, $^} ${SRC} ${LDFLAGS} -o $@

debug: clean
$(MAKE) CFLAGS_EXTRA='${CFLAGS_EXTRA} ${CFLAGS_DEBUG}'

coverage: clean
$(MAKE) CFLAGS_EXTRA='--coverage'

asan: clean
$(MAKE) CFLAGS_EXTRA='-fsanitize=address'

ubsan: clean
$(MAKE) CFLAGS_EXTRA='-fsanitize=undefined'

msan: clean
$(MAKE) CFLAGS_EXTRA='-fsanitize=memory -fsanitize-memory-track-origins'

valgrind: clean ${ALL}
@for test in ${ALL}; do \
valgrind --leak-check=full --log-file="$$test.valgrind" "./$$test"; \
cat "$$test.valgrind"; \
grep LEAK "$$test.valgrind" >/dev/null && exit 1 || true; \
done

tis: clean
$(MAKE) CC="tis-interpreter.sh --cc" CFLAGS='"${CFLAGS} ${CFLAGS_STD} -DHAVE_MEMRCHR=0 -DTIS_INTERPRETER=1"' CFLAGS_STD='' CFLAGS_LIBC='' LDFLAGS='#' $(ALL)

clean:
@echo cleaning
@rm -f ccan-config config.h
@rm -f data symlink hardlink
@rm -f $(ALL)
@rm -f *.gcov *.gcda *.gcno
@rm -f *.valgrind

.PHONY: clean debug coverage tis valgrind asan ubsan msan
9 changes: 9 additions & 0 deletions test/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Unit tests for the low level routines used by vis
-------------------------------------------------

The testing infrastruce makes use of the tap module from the
[C Code Archive](http://ccodearchive.net).

To run the tests, execute `make`.

$ make
213 changes: 213 additions & 0 deletions test/core/array-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "tap.h"
#include "array.h"
#include "util.h"

typedef struct {
char key[64];
int value;
} Item;

static int values[] = { 2, 3, 5, 7, 11 };
static const size_t len = LENGTH(values);

static bool item_compare(Item *a, Item *b) {
return strcmp(a->key, b->key) == 0 && a->value == b->value;
}

static void test_small_objects(void) {
Array arr;
array_init_sized(&arr, sizeof(int));
ok(array_length(&arr) == 0, "Initialization");
ok(!array_set(&arr, 0, NULL) && errno == EINVAL, "Set with invalid index");
ok(array_get(&arr, 0) == NULL && errno == EINVAL, "Get with invalid index");
ok(array_peek(&arr) == NULL && array_length(&arr) == 0, "Peek empty array");
ok(array_pop(&arr) == NULL && array_length(&arr) == 0, "Pop empty array");

for (size_t i = 0; i < len; i++) {
int *v;
ok(array_add(&arr, &values[i]) && array_length(&arr) == i+1,
"Add integer: %zu = %d", i, values[i]);
ok((v = array_get(&arr, i)) && *v == values[i],
"Get integer: %zu = %d", i, *v);
}

for (size_t i = 0; i < len; i++) {
ok(array_set(&arr, i, &values[len-i-1]) && array_length(&arr) == len,
"Set array element: %zu = %d", i, values[len-i-1]);
}

for (size_t i = 0; i < len; i++) {
int *v;
ok((v = array_get(&arr, i)) && *v == values[len-i-1],
"Get array element: %zu = %d", i, *v);
}

int *v;
ok((v = array_peek(&arr)) && *v == values[0] && array_length(&arr) == len, "Peek populated array");
ok((v = array_pop(&arr)) && *v == values[0] && array_length(&arr) == len-1, "Pop populated array");
ok((v = array_peek(&arr)) && *v == values[1] && array_length(&arr) == len-1, "Peek after pop");

array_clear(&arr);
ok(array_length(&arr) == 0 && array_get(&arr, 0) == NULL && errno == EINVAL, "Clear");

for (size_t i = 0; i < len; i++) {
ok(array_add(&arr, &values[i]) && array_length(&arr) == i+1,
"Re-add integer: %zu = %d", i, values[i]);
}

int old, *tmp;
ok((tmp = array_get(&arr, 0)) && (old = *tmp) && array_set(&arr, 0, NULL) &&
array_get(&arr, 0) == tmp && *tmp == 0 && array_set(&arr, 0, &old) &&
array_get(&arr, 0) == tmp && *tmp == old, "Set array element NULL");
ok(!array_set(&arr, array_length(&arr), &values[0]) && errno == EINVAL, "Get past end of array");
ok(!array_get(&arr, array_length(&arr)) && errno == EINVAL, "Get past end of array");

ok(!array_remove(&arr, array_length(&arr)) && errno == EINVAL, "Remove past end of array");

size_t len_before = array_length(&arr);
ok(array_remove(&arr, 2) && array_length(&arr) == len_before-1 &&
(v = array_get(&arr, 0)) && *v == values[0] &&
(v = array_get(&arr, 1)) && *v == values[1] &&
(v = array_get(&arr, 2)) && *v == values[3] &&
(v = array_get(&arr, 3)) && *v == values[4],
"Remove element 2");

len_before = array_length(&arr);
ok(array_remove(&arr, 0) && array_length(&arr) == len_before-1 &&
(v = array_get(&arr, 0)) && *v == values[1] &&
(v = array_get(&arr, 1)) && *v == values[3] &&
(v = array_get(&arr, 2)) && *v == values[4],
"Remove first element");

len_before = array_length(&arr);
ok(array_remove(&arr, len_before-1) && array_length(&arr) == len_before-1 &&
(v = array_get(&arr, 0)) && *v == values[1] &&
(v = array_get(&arr, 1)) && *v == values[3],
"Remove last element");

array_release(&arr);
}

static void test_large_objects(void) {
Array arr;
array_init_sized(&arr, sizeof(Item));
ok(array_length(&arr) == 0 && array_get(&arr, 0) == NULL && errno == EINVAL,
"Initialization");

Item items[len];

for (size_t i = 0; i < len; i++) {
snprintf(items[i].key, sizeof items[i].key, "key: %zu", i);
items[i].value = values[i];
Item *item;
ok(array_add(&arr, &items[i]) && array_length(&arr) == i+1,
"Add item: %zu = { '%s' = %d }", i, items[i].key, items[i].value);
ok((item = array_get(&arr, i)) && item != &items[i] && item_compare(item, &items[i]),
"Get item: %zu = { '%s' = %d }", i, item->key, item->value);
}

for (size_t i = 0; i < len; i++) {
Item *item = &items[len-i-1];
ok(array_set(&arr, i, item) && array_length(&arr) == len,
"Set array element: %zu = { '%s' = %d }", i, item->key, item->value);
}

for (size_t i = 0; i < len; i++) {
Item *item;
ok((item = array_get(&arr, i)) && item != &items[len-i-1] && item_compare(item, &items[len-i-1]),
"Get item: %zu = { '%s' = %d }", i, item->key, item->value);
}

ok(!array_add_ptr(&arr, &items[0]) && errno == ENOTSUP && array_length(&arr) == len,
"Adding pointer to non pointer array");
ok(!array_set_ptr(&arr, 0, &items[0]) && errno == ENOTSUP && item_compare(array_get(&arr, 0), &items[len-1]),
"Setting pointer in non pointer array");

array_clear(&arr);
ok(array_length(&arr) == 0 && array_get(&arr, 0) == NULL && errno == EINVAL, "Clear");

array_release(&arr);
}

static void test_pointers(void) {

Array arr;

array_init_sized(&arr, 1);
ok(array_length(&arr) == 0 && array_get_ptr(&arr, 0) == NULL && errno == ENOTSUP,
"Initialization with size 1");

ok(!array_add_ptr(&arr, &arr) && errno == ENOTSUP && array_get_ptr(&arr, 0) == NULL,
"Add pointer to non-pointer array");

errno = 0;
char byte = '_', *ptr;
ok(array_add(&arr, &byte) && (ptr = array_get(&arr, 0)) && *ptr == byte,
"Add byte element");
ok(!array_get_ptr(&arr, 0) && errno == ENOTSUP, "Get pointer from non-pointer array");
array_release(&arr);

array_init(&arr);
ok(array_length(&arr) == 0 && array_get_ptr(&arr, 0) == NULL && errno == EINVAL,
"Initialization");

Item *items[len];

for (size_t i = 0; i < len; i++) {
items[i] = malloc(sizeof(Item));
snprintf(items[i]->key, sizeof(items[i]->key), "key: %zu", i);
items[i]->value = values[i];
}

for (size_t i = 0; i < len; i++) {
Item *item;
ok(array_add_ptr(&arr, items[i]) && array_length(&arr) == i+1,
"Add item: %zu = %p", i, (void*)items[i]);
ok((item = array_get_ptr(&arr, i)) && item == items[i],
"Get item: %zu = %p", i, (void*)item);
}

for (size_t i = 0; i < len; i++) {
Item *item = items[len-i-1];
ok(array_set_ptr(&arr, i, item) && array_length(&arr) == len,
"Set item: %zu = %p", i, (void*)item);
}

for (size_t i = 0; i < len; i++) {
Item *item;
ok((item = array_get_ptr(&arr, i)) && item == items[len-i-1],
"Get item: %zu = %p", i, (void*)item);
}

Item *tmp;
ok((tmp = array_get_ptr(&arr, 0)) && array_set_ptr(&arr, 0, NULL) &&
array_get_ptr(&arr, 0) == NULL && array_set_ptr(&arr, 0, tmp) &&
array_get_ptr(&arr, 0) == tmp, "Set pointer NULL");
ok(!array_set_ptr(&arr, array_length(&arr), items[0]) && errno == EINVAL, "Set pointer past end of array");
ok(!array_get_ptr(&arr, array_length(&arr)) && errno == EINVAL, "Get pointer past end of array");

array_clear(&arr);
ok(array_length(&arr) == 0 && array_get_ptr(&arr, 0) == NULL && errno == EINVAL, "Clear");

for (size_t i = 0; i < len; i++) {
ok(array_add_ptr(&arr, items[i]) && array_length(&arr) == i+1,
"Re-add item: %zu = %p", i, (void*)items[i]);
}
array_release_full(&arr);
}

int main(int argc, char *argv[]) {
plan_no_plan();

test_small_objects();
test_large_objects();
test_pointers();

return exit_status();
}
Loading

4 comments on commit b707402

@jeremybobbin
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason why you did this?
It's making rebasing a pain in the ass for me

@mcepl
Copy link
Contributor

@mcepl mcepl commented on b707402 Aug 9, 2024

Choose a reason for hiding this comment

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

Is there a reason why you did this? It's making rebasing a pain in the ass for me

You will rebase once, at least your branches will be fresh ;). Seriously, we got rid of one unnecessary repo, which should never have existed in the first place. This has been discussed a couple of times on the list (e.g., https://lists.sr.ht/~martanne/devel/%[email protected]%3E).

@jeremybobbin
Copy link
Contributor

Choose a reason for hiding this comment

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

If there's a bug, we can add the failing test in the test repo, but leave .gitmodules(for the main repo) untouched.
Example: martanne/vis-test@783b7ef

Additionally, imagine a feature & a test case(for that feature) were added in one commit to the main repo,
and that you were hunting for a bug that showed up some time before the feature, but vanished sometime after.

To determine the patch that fixed the bug, you'd need to add your test & move it to some commit before the series of commits you're testing.
and then either drop the feature-commit(so that the tests pass - handling whatever conflict may arise), or, pass some arguments to the test suite to omit that feature-specific test.

I believe a separate test repository, which tests only core functionality & is seldom modified, is the simplest strategy here.

Here are the arguments against git-submodules in the mailing list:

  • Linux, Python & LibreOffice, even with their complexity, don't use git-submodules
  • vis-test repo is locked-down
  • it's much easier to review new features when a test is included in the patchset

Regarding Linux, Python & LibreOffice not using submodules, this point could go either way.
If a test case is needed to supplement a feature's documentation, then one could work to simplify the feature or to simplify the documentation.

All of that aside, I really hope this change attracts new contributors.

@mcepl
Copy link
Contributor

@mcepl mcepl commented on b707402 Aug 11, 2024

Choose a reason for hiding this comment

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

@rnpnr Just to make you aware of this discussion, I am leaving it as it is, because I don’t think it as they say serves any purpose any more.

Please sign in to comment.