From: Michael D. Lowis Date: Fri, 27 Oct 2017 14:47:41 +0000 (-0400) Subject: moved unit tests to separate modules and folder X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=4cd1dc8db7d9c5dd7a64b88e84d6ce354ec6dfb7;p=archive%2Ftide-ocaml.git moved unit tests to separate modules and folder --- diff --git a/Makefile b/Makefile index a904b9a..e5c12f8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Toolchain Configuration #------------------------------------------------------------------------------- -INCS = -I . -I lib -I /usr/X11R6/include -I /usr/include/freetype2 -I /usr/X11R6/include/freetype2 +INCS = -I . -I lib -I tests -I /usr/X11R6/include -I /usr/include/freetype2 -I /usr/X11R6/include/freetype2 LIBS = -L/usr/X11R6/lib -lX11 -lXft -lfontconfig ifeq ($(NATIVE), 1) @@ -23,9 +23,8 @@ endif # Target Definitions #------------------------------------------------------------------------------- -BINS = edit tests +BINS = edit unittests LIBOBJS = \ - lib/test.$(OBJEXT) \ lib/misc.$(OBJEXT) \ lib/x11.$(OBJEXT) \ lib/cfg.$(OBJEXT) \ @@ -37,17 +36,22 @@ LIBOBJS = \ lib/misc_prims.o \ lib/utf8.o +TESTOBJS = \ + tests/test.$(OBJEXT) \ + tests/rope_tests.$(OBJEXT) \ + tests/scrollmap_tests.$(OBJEXT) + .PHONY: all clean all: $(BINS) - ./tests + ./unittests clean: $(RM) deps.mk $(BINS) *.cm* *.o *.a *.so lib/*.cm* lib/*.o # Executable targets edit: tide.$(LIBEXT) edit.$(OBJEXT) -tests: tide.$(LIBEXT) tests.$(OBJEXT) +unittests: tide.$(LIBEXT) $(TESTOBJS) unittests.$(OBJEXT) # Library targets tide.$(LIBEXT): $(LIBOBJS) diff --git a/lib/buf.ml b/lib/buf.ml index acade5f..c19f6d8 100644 --- a/lib/buf.ml +++ b/lib/buf.ml @@ -17,9 +17,3 @@ let iter_from fn buf i = let iteri_from fn buf i = Rope.iteri_from fn buf.rope i - -(* Unit Tests *****************************************************************) - -let run_unit_tests () = - let open Test in - () diff --git a/lib/rope.ml b/lib/rope.ml index 9c6f076..2423259 100644 --- a/lib/rope.ml +++ b/lib/rope.ml @@ -135,151 +135,6 @@ let gets rope i j = let to_string rope = gets rope 0 (length rope) -(* Unit Tests *****************************************************************) - -let run_unit_tests () = - let open Test in - (* length() tests *) - test "length : 0 for empty string" (fun () -> - let rope = Leaf("", 0, 0) in - assert( length rope == 0 ) - ); - test "length : equal to length of leaf" (fun () -> - let rope = Leaf("a", 0, 1) in - assert( length rope == 1 ) - ); - test "length : equal to sum of leaf lengths" (fun () -> - let rope = (join (Leaf("a", 0, 1)) (Leaf("a", 0, 1))) in - assert( length rope == 2 ) - ); - - (* join() tests *) - test "join : join two leaves into rope" (fun () -> - let left = Leaf("a", 0, 1) in - let right = Leaf("a", 0, 1) in - let rope = (join left right) in - assert( match rope with - | Node (l,r,2) -> (l == left && r == right) - | _ -> false) - ); - test "join : join a rope with a leaf (l to r)" (fun () -> - let left = join (Leaf("a", 0, 1)) (Leaf("a", 0, 1)) in - let right = Leaf("a", 0, 1) in - let rope = (join left right) in - assert( match rope with - | Node (l,r,3) -> (l == left && r == right) - | _ -> false) - ); - test "join : join a rope with a leaf (r to l)" (fun () -> - let left = Leaf("a", 0, 1) in - let right = join (Leaf("a", 0, 1)) (Leaf("a", 0, 1)) in - let rope = (join left right) in - assert( match rope with - | Node (l,r,3) -> (l == left && r == right) - | _ -> false) - ); - - (* getc() tests *) - test "getc : raise Out_of_bounds on negative index" (fun () -> - let rope = Leaf("a", 0, 1) in - try getc rope (-1); assert false - with Out_of_bounds _ -> assert true - ); - test "getc : raise Out_of_bounds on out of bounds index" (fun () -> - let rope = Leaf("a", 0, 1) in - try getc rope (2); assert false - with Out_of_bounds _ -> assert true - ); - test "getc : return index 0 of leaf" (fun () -> - let rope = Leaf("abc", 0, 3) in - assert( (getc rope (0)) == Char.code 'a' ); - ); - test "getc : return index 1 of leaf" (fun () -> - let rope = Leaf("abc", 0, 3) in - assert( (getc rope (1)) == Char.code 'b' ); - ); - test "getc : return index 2 of leaf" (fun () -> - let rope = Leaf("abc", 0, 3) in - assert( (getc rope (2)) == Char.code 'c' ); - ); - test "getc : return index 0 of rope" (fun () -> - let rope = Node((Leaf("a", 0, 1)), (Leaf("b", 0, 1)), 2) in - assert( (getc rope (0)) == Char.code 'a' ); - ); - test "getc : return index 1 of rope" (fun () -> - let rope = Node((Leaf("a", 0, 1)), (Leaf("b", 0, 1)), 2) in - assert( (getc rope (1)) == Char.code 'b' ); - ); - - (* puts() tests *) - test "puts : insert at index 0" (fun () -> - let rope = Leaf("bc", 0, 2) in - let rope = (puts rope "a" 0) in - assert( (length rope) == 3 ); - assert( (getc rope (0)) == Char.code 'a' ); - assert( (getc rope (1)) == Char.code 'b' ); - assert( (getc rope (2)) == Char.code 'c' ); - ); - test "puts : insert at index 1" (fun () -> - let rope = Leaf("ac", 0, 2) in - let rope = (puts rope "b" 1) in - assert( (length rope) == 3 ); - assert( (getc rope (0)) == Char.code 'a' ); - assert( (getc rope (1)) == Char.code 'b' ); - assert( (getc rope (2)) == Char.code 'c' ); - ); - test "puts : insert index at 2" (fun () -> - let rope = Leaf("ab", 0, 2) in - let rope = (puts rope "c" 2) in - assert( (length rope) == 3 ); - assert( (getc rope (0)) == Char.code 'a' ); - assert( (getc rope (1)) == Char.code 'b' ); - assert( (getc rope (2)) == Char.code 'c' ); - ); - - (* is_bol() tests *) - test "is_bol : should return true if pos is 0" (fun () -> - let rope = Leaf("abc", 0, 3) in - assert( is_bol rope 0 ); - ); - test "is_bol : should return true if pos is first char of line" (fun () -> - let rope = Leaf("\nabc", 0, 3) in - assert( is_bol rope 1 ); - ); - test "is_bol : should return false if pos is not first char of line" (fun () -> - let rope = Leaf("\nabc", 0, 3) in - assert( (is_bol rope 2) == false ); - ); - test "is_bol : should return false if previous char is not \n" (fun () -> - let rope = Leaf("\rabc", 0, 3) in - assert( (is_bol rope 1) == false ); - ); - - (* is_eol() tests *) - test "is_eol : should return true if pos is Rope.last" (fun () -> - let rope = Leaf("abc", 0, 3) in - assert( is_eol rope 2 ); - ); - test "is_eol : should return true if pos is last char of line with \n ending" (fun () -> - let rope = Leaf("abc\n", 0, 4) in - assert( is_eol rope 2 ); - ); - test "is_eol : should return true if pos is last char of line with \r\n ending" (fun () -> - let rope = Leaf("abc\r\n", 0, 5) in - assert( is_eol rope 3 ); - ); - test "is_eol : should return false if pos is not last char of line" (fun () -> - let rope = Leaf("abcd\n", 0, 5) in - assert( (is_eol rope 2) == false ); - ); - - (* to_bol() tests *) - test "to_bol : should return index of first char on the line" (fun () -> - let rope = Leaf("\nabc\n", 0, 5) in - assert( (to_bol rope 2) == 1 ); - ); - () - (* size_t buf_lscan(Buf* buf, size_t pos, Rune r); diff --git a/lib/scrollmap.ml b/lib/scrollmap.ml index b180ae8..12b27fe 100644 --- a/lib/scrollmap.ml +++ b/lib/scrollmap.ml @@ -14,7 +14,6 @@ let make buf width off = let csr = Draw.Cursor.make (width, 0) 0 0 in let bol = (Rope.to_bol (Buf.rope buf) off) in let lines = ref [bol] in - let csr = Draw.Cursor.make (width, 0) 0 0 in let process_glyph i c = if (Draw.Cursor.next_glyph csr c) then lines := i :: !lines; @@ -26,10 +25,6 @@ let make buf width off = { width = width; lines = lines; index = index } let first map = - (* - Printf.printf "%d: %d" map.index map.lines.(map.index); - print_endline ""; - *) map.lines.(map.index) let bopl buf off = diff --git a/tests.ml b/tests.ml deleted file mode 100644 index def58ab..0000000 --- a/tests.ml +++ /dev/null @@ -1,4 +0,0 @@ -let () = - Rope.run_unit_tests (); - Scrollmap.run_unit_tests (); - Test.report_results () diff --git a/tests/rope_tests.ml b/tests/rope_tests.ml new file mode 100644 index 0000000..c5d0a1b --- /dev/null +++ b/tests/rope_tests.ml @@ -0,0 +1,144 @@ +open Test +open Rope + +let () = + (* length() tests *) + test "length : 0 for empty string" (fun () -> + let rope = Leaf("", 0, 0) in + assert( length rope == 0 ) + ); + test "length : equal to length of leaf" (fun () -> + let rope = Leaf("a", 0, 1) in + assert( length rope == 1 ) + ); + test "length : equal to sum of leaf lengths" (fun () -> + let rope = (join (Leaf("a", 0, 1)) (Leaf("a", 0, 1))) in + assert( length rope == 2 ) + ); + + (* join() tests *) + test "join : join two leaves into rope" (fun () -> + let left = Leaf("a", 0, 1) in + let right = Leaf("a", 0, 1) in + let rope = (join left right) in + assert( match rope with + | Node (l,r,2) -> (l == left && r == right) + | _ -> false) + ); + test "join : join a rope with a leaf (l to r)" (fun () -> + let left = join (Leaf("a", 0, 1)) (Leaf("a", 0, 1)) in + let right = Leaf("a", 0, 1) in + let rope = (join left right) in + assert( match rope with + | Node (l,r,3) -> (l == left && r == right) + | _ -> false) + ); + test "join : join a rope with a leaf (r to l)" (fun () -> + let left = Leaf("a", 0, 1) in + let right = join (Leaf("a", 0, 1)) (Leaf("a", 0, 1)) in + let rope = (join left right) in + assert( match rope with + | Node (l,r,3) -> (l == left && r == right) + | _ -> false) + ); + + (* getc() tests *) + test "getc : raise Out_of_bounds on negative index" (fun () -> + let rope = Leaf("a", 0, 1) in + try getc rope (-1); assert false + with Out_of_bounds _ -> assert true + ); + test "getc : raise Out_of_bounds on out of bounds index" (fun () -> + let rope = Leaf("a", 0, 1) in + try getc rope (2); assert false + with Out_of_bounds _ -> assert true + ); + test "getc : return index 0 of leaf" (fun () -> + let rope = Leaf("abc", 0, 3) in + assert( (getc rope (0)) == Char.code 'a' ); + ); + test "getc : return index 1 of leaf" (fun () -> + let rope = Leaf("abc", 0, 3) in + assert( (getc rope (1)) == Char.code 'b' ); + ); + test "getc : return index 2 of leaf" (fun () -> + let rope = Leaf("abc", 0, 3) in + assert( (getc rope (2)) == Char.code 'c' ); + ); + test "getc : return index 0 of rope" (fun () -> + let rope = Node((Leaf("a", 0, 1)), (Leaf("b", 0, 1)), 2) in + assert( (getc rope (0)) == Char.code 'a' ); + ); + test "getc : return index 1 of rope" (fun () -> + let rope = Node((Leaf("a", 0, 1)), (Leaf("b", 0, 1)), 2) in + assert( (getc rope (1)) == Char.code 'b' ); + ); + + (* puts() tests *) + test "puts : insert at index 0" (fun () -> + let rope = Leaf("bc", 0, 2) in + let rope = (puts rope "a" 0) in + assert( (length rope) == 3 ); + assert( (getc rope (0)) == Char.code 'a' ); + assert( (getc rope (1)) == Char.code 'b' ); + assert( (getc rope (2)) == Char.code 'c' ); + ); + test "puts : insert at index 1" (fun () -> + let rope = Leaf("ac", 0, 2) in + let rope = (puts rope "b" 1) in + assert( (length rope) == 3 ); + assert( (getc rope (0)) == Char.code 'a' ); + assert( (getc rope (1)) == Char.code 'b' ); + assert( (getc rope (2)) == Char.code 'c' ); + ); + test "puts : insert index at 2" (fun () -> + let rope = Leaf("ab", 0, 2) in + let rope = (puts rope "c" 2) in + assert( (length rope) == 3 ); + assert( (getc rope (0)) == Char.code 'a' ); + assert( (getc rope (1)) == Char.code 'b' ); + assert( (getc rope (2)) == Char.code 'c' ); + ); + + (* is_bol() tests *) + test "is_bol : should return true if pos is 0" (fun () -> + let rope = Leaf("abc", 0, 3) in + assert( is_bol rope 0 ); + ); + test "is_bol : should return true if pos is first char of line" (fun () -> + let rope = Leaf("\nabc", 0, 3) in + assert( is_bol rope 1 ); + ); + test "is_bol : should return false if pos is not first char of line" (fun () -> + let rope = Leaf("\nabc", 0, 3) in + assert( (is_bol rope 2) == false ); + ); + test "is_bol : should return false if previous char is not \n" (fun () -> + let rope = Leaf("\rabc", 0, 3) in + assert( (is_bol rope 1) == false ); + ); + + (* is_eol() tests *) + test "is_eol : should return true if pos is Rope.last" (fun () -> + let rope = Leaf("abc", 0, 3) in + assert( is_eol rope 2 ); + ); + test "is_eol : should return true if pos is last char of line with \n ending" (fun () -> + let rope = Leaf("abc\n", 0, 4) in + assert( is_eol rope 2 ); + ); + test "is_eol : should return true if pos is last char of line with \r\n ending" (fun () -> + let rope = Leaf("abc\r\n", 0, 5) in + assert( is_eol rope 3 ); + ); + test "is_eol : should return false if pos is not last char of line" (fun () -> + let rope = Leaf("abcd\n", 0, 5) in + assert( (is_eol rope 2) == false ); + ); + + (* to_bol() tests *) + test "to_bol : should return index of first char on the line" (fun () -> + let rope = Leaf("\nabc\n", 0, 5) in + assert( (to_bol rope 2) == 1 ); + ); + () diff --git a/tests/scrollmap_tests.ml b/tests/scrollmap_tests.ml new file mode 100644 index 0000000..343efaa --- /dev/null +++ b/tests/scrollmap_tests.ml @@ -0,0 +1,2 @@ +open Test +open Scrollmap diff --git a/lib/test.ml b/tests/test.ml similarity index 100% rename from lib/test.ml rename to tests/test.ml diff --git a/unittests.ml b/unittests.ml new file mode 100644 index 0000000..7f3f2fe --- /dev/null +++ b/unittests.ml @@ -0,0 +1,5 @@ +open Rope_tests +open Scrollmap_tests + +let () = + Test.report_results ()