Permalink
Browse files

WIP Quickcheck properties for combinators and memoization.

  • Loading branch information...
1 parent edbf0e0 commit 0e92ec97fdf84eb91ebd2be96bdb8158f3e8c594 @seancribbs committed Dec 8, 2010
Showing with 104 additions and 2 deletions.
  1. +1 −2 .gitignore
  2. +24 −0 eqc/memo_eqc.erl
  3. +79 −0 eqc/peg_eqc.erl
View
@@ -1,4 +1,3 @@
-ebin/*.beam
-extra/*.beam
+*.beam
*#*
.eunit
View
@@ -0,0 +1,24 @@
+%%% @author Sean Cribbs <sean@seanbasho>
+%%% @copyright (C) 2010, Sean Cribbs
+%%% @doc
+%%%
+%%% @end
+%%% Created : 27 Apr 2010 by Sean Cribbs <sean@seanbasho>
+
+-module(memo_eqc).
+
+
+-include_lib("eqc/include/eqc.hrl").
+-include_lib("eqc/include/eqc_statem.hrl").
+
+-compile(export_all).
+
+parse_fun() ->
+ oneof([
+ fun(Inp,Idx) ->
+ {fail, {expected, pass, Idx}}
+ end,
+ fun(Inp,Idx) ->
+ {[], Inp, Idx}
+ end
+ ]).
View
@@ -0,0 +1,79 @@
+-module(peg_eqc).
+-compile(export_all).
+-include_lib("eqc/include/eqc.hrl").
+
+prop_charclass_matches() ->
+ ?FORALL({String,Index},
+ {non_empty(ascii_binary()), index()},
+ begin
+ Fun = neotoma_peg:p_charclass(charclass(String)),
+ case Fun(String,Index) of
+ {M,R,_I} when is_binary(M), is_binary(R) -> true;
+ {fail, _} -> false
+ end
+ end).
+
+
+prop_string() ->
+ conjunction([{matches_self,prop_string_matches_self()},
+ {matches_head,prop_string_matches_head()},
+ {does_not_match,prop_string_does_not_match()}]).
+
+prop_string_does_not_match() ->
+ ?FORALL(String, non_empty(utf8_binary()),
+ ?FORALL({Input, Index}, {non_matching_binary(String), index()},
+ begin
+ {fail, {expected, {string, String}, _}} = (neotoma_peg:p_string(String))(Input, Index),
+ true
+ end)).
+
+prop_string_matches_head() ->
+ ?FORALL({String,Tail,Index}, {utf8_binary(),utf8_binary(),index()},
+ begin
+ {String, Tail, _} = (neotoma_peg:p_string(String))(iolist_to_binary([String,Tail]), Index),
+ true
+ end).
+
+prop_string_matches_self() ->
+ ?FORALL({String,Index}, {utf8_binary(),index()},
+ begin
+ {String, <<>>, _} = (neotoma_peg:p_string(String))(String, Index),
+ true
+ end).
+
+%% Generators
+charclass(String) ->
+ String2 = re:replace(String, "(\\[|\\])","\\&", [global, {return, binary}]),
+ iolist_to_binary(["[",String2,"]"]).
+
+index() ->
+ {{line, nat()},{column, nat()}}.
+
+ascii_char() ->
+ choose(0,255).
+
+ascii_string() ->
+ list(ascii_char()).
+
+ascii_binary() ->
+ ?LET(L, ascii_string(), list_to_binary(L)).
+
+utf8_char() ->
+ choose(0,1024).
+
+utf8_charlist() ->
+ list(utf8_char()).
+
+utf8_binary() ->
+ ?SUCHTHAT(S, ?LET(L, utf8_charlist(), unicode:characters_to_binary(L,utf8,utf8)), is_binary(S)).
+
+non_matching_binary(String) ->
+ ?SUCHTHAT(Input,utf8_binary(),
+ begin
+ Size = erlang:byte_size(String),
+ case Input of
+ <<>> -> false;
+ <<String:Size/binary, _Rest/binary>> -> false;
+ _ -> Size > 0
+ end
+ end).

0 comments on commit 0e92ec9

Please sign in to comment.