Guest User

Untitled

a guest
Sep 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. -module(reduced_test_case).
  2.  
  3. -define(NODEBUG, 1).
  4. -include_lib("eunit/include/eunit.hrl").
  5.  
  6. -behavior(gen_server).
  7.  
  8. %-compile(export_all).
  9. -export([init/1, code_change/3, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
  10. -export([start/0]).
  11.  
  12. -define(SERVER, ?MODULE).
  13.  
  14. start() ->
  15. {ok, PID} = gen_server:start_link({local, ?SERVER}, ?MODULE, [], []),
  16. PID ! {hi, [1040,1044]}, % try a string/list
  17. PID ! {hi, <<"\xa9">>}, % try a latin1 binary
  18. PID ! {hi, <<"\xd0\x90\xd0\x94">>}, % try a utf8 binary
  19. PID ! {hi, [[1040,1044], <<"\xa9">>, <<"\xd0\x90\xd0\x94">>]}, % all together in a iolist
  20. {ok, PID}.
  21.  
  22.  
  23. to_binary(IoList) when is_binary(IoList) ->
  24. IoList;
  25.  
  26. to_binary(IoList) ->
  27. to_binary(IoList, []).
  28.  
  29. to_binary([], Acc) ->
  30. Acc;
  31.  
  32. to_binary([Head | Tail], Acc) when is_binary(Head) ->
  33. to_binary(Tail, [Acc | Head]);
  34.  
  35. to_binary(IoList, Acc) ->
  36. case unicode:characters_to_binary(IoList) of
  37. {error, Bin, Rest} ->
  38. to_binary(Rest, [Acc|Bin]);
  39. {incomplete, Bin1, Bin2} ->
  40. [Acc | [Bin1, Bin2]];
  41. Bin ->
  42. Bin
  43. end.
  44.  
  45. print(Data) ->
  46. port_command(stdout, [to_binary(Data), "\n"]).
  47.  
  48. init(_) ->
  49. StdOut = open_port("/dev/stdout", [binary, out]),
  50. register(stdout, StdOut),
  51. {ok, []}.
  52.  
  53.  
  54. handle_info({hi, Data}, State) ->
  55. print(Data),
  56. {noreply, State};
  57.  
  58.  
  59. handle_info(_Msg, State) -> {noreply, State}.
  60. handle_call(_Msg, _Caller, State) -> {noreply, State}.
  61. handle_cast(_Msg, State) -> {noreply, State}.
  62. terminate(_Reason, _State) -> ok.
  63. code_change(_OldVersion, State, _Extra) -> {ok, State}.
Add Comment
Please, Sign In to add comment