Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Forked from manpages/reduced_test_case.erl
Created March 31, 2012 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdamjan/2258609 to your computer and use it in GitHub Desktop.
Save gdamjan/2258609 to your computer and use it in GitHub Desktop.
Yet Another Erlang/Unicode Fuckup
-module(reduced_test_case).
-define(NODEBUG, 1).
-include_lib("eunit/include/eunit.hrl").
-behavior(gen_server).
%-compile(export_all).
-export([init/1, code_change/3, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
-export([start/0]).
-define(SERVER, ?MODULE).
start() ->
{ok, PID} = gen_server:start_link({local, ?SERVER}, ?MODULE, [], []),
PID ! {hi, [1040,1044]}, % try a string/list
PID ! {hi, <<"\xa9">>}, % try a latin1 binary
PID ! {hi, <<"\xd0\x90\xd0\x94">>}, % try a utf8 binary
PID ! {hi, [[1040,1044], <<"\xa9">>, <<"\xd0\x90\xd0\x94">>]}, % all together in a iolist
{ok, PID}.
to_binary(IoList) when is_binary(IoList) ->
IoList;
to_binary(IoList) ->
to_binary(IoList, []).
to_binary([], Acc) ->
Acc;
to_binary([Head | Tail], Acc) when is_binary(Head) ->
to_binary(Tail, [Acc | Head]);
to_binary(IoList, Acc) ->
case unicode:characters_to_binary(IoList) of
{error, Bin, Rest} ->
to_binary(Rest, [Acc|Bin]);
{incomplete, Bin1, Bin2} ->
[Acc | [Bin1, Bin2]];
Bin ->
Bin
end.
print(Data) ->
port_command(stdout, [to_binary(Data), "\n"]).
init(_) ->
StdOut = open_port("/dev/stdout", [binary, out]),
register(stdout, StdOut),
{ok, []}.
handle_info({hi, Data}, State) ->
print(Data),
{noreply, State};
handle_info(_Msg, State) -> {noreply, State}.
handle_call(_Msg, _Caller, State) -> {noreply, State}.
handle_cast(_Msg, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVersion, State, _Extra) -> {ok, State}.
echo "Reduced test case:"
cat reduced_test_case.erl
erlc reduced_test_case.erl
echo
echo "==="
echo "Running erl -eval \"reduced_test_case:start()\" with keys"
echo "* -run init stop"
echo
echo "<output>"
erl -eval "reduced_test_case:start()" -run init stop
echo "</output>"
echo
echo
echo "==="
echo "Running erl -eval \"reduced_test_case:start()\" with keys"
echo "* -run init stop -noshell"
echo
echo "<output>"
erl -eval "reduced_test_case:start()" -run init stop -noshell
echo "</output>"
echo
echo
echo
echo "Feel the difference."
@manpages
Copy link

Wow, you have pushed it even farther then I thought you will.
Thanks for forking the original gist again! I still owe you a beer :)

@gdamjan
Copy link
Author

gdamjan commented Apr 28, 2012

be careful though, that to_binary recursive function has a bug where it enters an infinite loop in some cases. In the end I've decided not to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment