<commit_before>-module(egeoip_tests).
-include_lib("eunit/include/eunit.hrl").
-include("egeoip.hrl").

address_fast_test_() ->
    [?_assertEqual(invalid_fast_address, egeoip:address_fast("1", 0, 24)),
     ?_assertEqual(16909060, egeoip:address_fast("1.2.3.4", 0, 24)),
     ?_assertEqual(3573612662, egeoip:address_fast("172.16.17.32", 0 , 24))].

run_test_() ->
    {inorder,
     {foreach,
      fun egeoip:start/0,
      fun(_) -> egeoip:stop() end,
      [{"egeoip_bench", fun egeoip_bench/0},
       {"egeoip", fun egeoip/0},
       {"egeoip_fail", fun egeoip_fail/0},
       {"egeoip_lookup", fun egeoip_lookup/0},
       {"egeoip_reserved", {generator, fun egeoip_reserved_gen/0}},
       {"country_test", {generator, fun country_test_gen/0}},
       {"country_test2", {generator, fun country_test2_gen/0}},
       {"non_parallel", fun non_parallel/0}]}}.

egeoip_reserved_gen() ->
    %% We don't test all of them, just a few.
    [?_assertMatch(
        {ok, #geoip{country_code = "",
                    country_code3 = "",
                    country_name = "",
                    region = <<>>,
                    city = <<>>,
                    postal_code = <<>>,
                    area_code = 0,
                    dma_code = 0}},
       egeoip:lookup(Ip))
     || Ip <- ["0.0.0.1", "127.0.0.1", "10.0.0.1", "192.168.0.1"]].

egeoip_bench() ->
    ?assertMatch(
       {_, _},
       egeoip:bench(1)),
    ok.

egeoip() ->
    {ok, IpAddressLong} = egeoip:ip2long({207,145,216,106}),
    {ok, IpAddressLong} = egeoip:ip2long("192.168.3.11"),
    {ok, IpAddressLong} = egeoip:ip2long(<<207,145,216,106>>),
    {ok, R} = egeoip:lookup(IpAddressLong),
    #geoip{country_code = "US",
           country_code3 = "USA",
           country_name = "United States",
           region = <<"CA">>,
           _ = _} = R,
    %% This is the test IP that MaxMind uses
    {ok, R1} = egeoip:lookup("24.24.24.24"),
    #geoip{country_code = "US",
           country_code3 = "USA",
           country_name = "United States",
           region = <<"NY">>,
           _ = _} = R1.


egeoip_fail() ->
    ?assertMatch({ok, #geoip{country_code = "",
                             country_code3 = "",
                             country_name = "",
                             region = <<>>,
                             city = <<>>,
                             postal_code = <<>>,
                             area_code = 0,
                             dma_code = 0}}, egeoip:lookup("2")),
    ok.

egeoip_lookup() ->
    {ok, R1} = egeoip:lookup("24.24.24.24"),
    {ok, R2} = egeoip:lookup({24,24,24,24}),
    ?assertEqual(R1,R2).

non_parallel() ->
    %% recreate the non-parallelized version of egeoip and then verify
    %% that the upgrade works.
    Workers = [Egeoip | T] = tuple_to_list(egeoip_sup:worker_names()),
    %% Remove all worker processes except for the first one
    lists:map(fun(Worker) ->
                      ok = supervisor:terminate_child(egeoip_sup, Worker),
                      ok = supervisor:delete_child(egeoip_sup, Worker)
              end, T),
    Pid = whereis(Egeoip),
    unregister(Egeoip),
    register(egeoip, Pid),
    ?assert(Pid == whereis(egeoip)),
    [?assert(undefined == whereis(W)) || W <- Workers],
    %% Should upgrade when calling lookup
    {ok, _R} = egeoip:lookup("172.16.58.3"),
    ?assert(undefined == whereis(egeoip)),
    [?assertNot(undefined == whereis(W)) || W <- Workers].

no_egeoip_test() ->
    Lookup = {lookup, "172.16.58.3"},
    ?assertExit({noproc,{gen_server,call,[egeoip,Lookup]}},gen_server:call(egeoip, Lookup)).

country_test_gen() ->
    [{Ip ++ " -> " ++ CC ++ " / " ++ C3,
      ?_assertMatch(
         {ok, #geoip{country_code=CC, country_code3=C3}},
         egeoip:lookup(Ip))}
     || {Ip, CC, C3} <- country_test_data()].

country_test2_gen() ->
    [{Ip ++ " -> " ++ CC,
      ?_assertMatch(
         {ok, #geoip{country_code=CC}},
         egeoip:lookup(Ip))}
     || {Ip, CC} <- country_test2_data()].

country_test2_data() ->
    [{"192.168.127.12", "JO"},
     {"172.16.31.10", "US"},
     {"172.16.17.32", "AU"},
     {"1.2.3.4", "AU"},
     {"172.16.58.3", "SA"},
     {"192.168.3.11", "US"},
     {"172.16.58.3", "SA"},
     {"172.16.31.10", "US"},
     {"172.16.58.3", "US"},
     {"172.16.17.32", "IN"},
     {"192.168.3.11", "CY"},
     {"192.168.3.11", "MX"},
     {"192.168.127.12", "IN"},
     {"192.168.127.12", "PK"},
     {"172.16.17.32", "US"},
     {"192.168.3.11", "UA"},
     {"172.16.17.32", "GB"},
     {"172.16.17.32", "US"},
     {"192.168.3.11", "US"},
     {"172.16.31.10", "BE"},
     {"172.16.17.32", "PL"},
     {"192.168.127.12", "TH"},
     {"172.16.31.10", "US"},
     {"172.16.31.10", "US"},
     {"192.168.3.11", "IN"},
     {"172.16.58.3", "IN"},
     {"172.16.17.32", "ES"},
     {"172.16.31.10", "IL"},
     {"172.16.58.3", "GB"},
     {"192.168.3.11", "NL"}].

country_test_data() ->
    [{"172.16.58.3", "US", "USA"},
     {"192.168.127.12", "IT", "ITA"},
     {"192.168.3.11", "CZ", "CZE"},
     {"172.16.17.32", "JP", "JPN"},
     {"172.16.31.10", "FR", "FRA"},
     {"172.16.17.32", "BR", "BRA"},
     {"192.168.3.11", "DE", "DEU"},
     {"172.16.58.3", "BE", "BEL"},
     {"172.16.17.32", "IT", "ITA"},
     {"172.16.17.32", "AU", "AUS"},
     {"192.168.127.12", "ZA", "ZAF"},
     {"172.16.58.3", "IT", "ITA"},
     {"192.168.127.12", "IT", "ITA"},
     {"172.16.31.10", "FR", "FRA"},
     {"192.168.127.12", "TR", "TUR"},
     {"172.16.31.10", "KR", "KOR"},
     {"172.16.17.32", "KR", "KOR"},
     {"172.16.17.32", "MA", "MAR"},
     {"192.168.127.12", "DE", "DEU"},
     {"192.168.3.11", "DE", "DEU"},
     {"192.168.127.12", "HU", "HUN"},
     {"172.16.31.10", "DE", "DEU"},
     {"192.168.127.12", "IT", "ITA"},
     {"192.168.127.12", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"192.168.3.11", "IT", "ITA"},
     {"172.16.58.3", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"192.168.127.12", "IT", "ITA"},
     {"172.16.58.3", "IT", "ITA"},
     {"172.16.58.3", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"172.16.58.3", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"192.168.127.12", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.58.3", "IT", "ITA"},
     {"172.16.31.10", "FR", "FRA"},
     {"192.168.127.12", "TR", "TUR"},
     {"172.16.17.32", "TR", "TUR"},
     {"172.16.31.10", "JP", "JPN"},
     {"172.16.58.3", "JP", "JPN"},
     {"172.16.17.32", "JP", "JPN"},
     {"192.168.127.12", "JP", "JPN"},
     {"172.16.31.10", "KR", "KOR"},
     {"172.16.17.32", "JP", "JPN"},
     {"172.16.31.10", "FR", "FRA"},
     {"172.16.17.32", "BR", "BRA"},
     {"192.168.3.11", "ID", "IDN"},
     {"172.16.31.10", "US", "USA"},
     {"192.168.127.12", "US", "USA"},
     {"172.16.31.10", "US", "USA"},
     {"172.16.31.10", "MY", "MYS"},
     {"172.16.17.32", "AU", "AUS"},
     {"192.168.127.12", "AU", "AUS"},
     {"172.16.31.10", "JP", "JPN"},
     {"172.16.58.3", "SG", "SGP"},
     {"172.16.58.3", "GB", "GBR"},
     {"172.16.17.32", "US", "USA"},
     {"172.16.31.10", "US", "USA"},
     {"172.16.58.3", "US", "USA"}].
<commit_msg>adapted tests for maxmind's country database<commit_after>-module(egeoip_tests).
-include_lib("eunit/include/eunit.hrl").
-include("egeoip.hrl").

address_fast_test_() ->
    [?_assertEqual(invalid_fast_address, egeoip:address_fast("1", 0, 24)),
     ?_assertEqual(16909060, egeoip:address_fast("1.2.3.4", 0, 24)),
     ?_assertEqual(3573612662, egeoip:address_fast("172.16.17.32", 0 , 24))].

run_test_() ->
    {inorder,
     {foreach,
      fun egeoip:start/0,
      fun(_) -> egeoip:stop() end,
      [{"egeoip_bench", fun egeoip_bench/0},
       {"egeoip", fun egeoip/0},
       {"egeoip_fail", fun egeoip_fail/0},
       {"egeoip_lookup", fun egeoip_lookup/0},
       {"egeoip_reserved", {generator, fun egeoip_reserved_gen/0}},
       {"country_test", {generator, fun country_test_gen/0}},
       {"country_test2", {generator, fun country_test2_gen/0}},
       {"country_test_countrydb", {generator, fun country_test_gen/0}},
       {"non_parallel", fun non_parallel/0}]}}.

run_countrydb_test_() ->
  {inorder,
     {foreach,
      fun() -> egeoip:start(country) end,
      fun(_) -> egeoip:stop() end,
      [{"egeoip_bench", fun egeoip_bench/0},
       {"egeoip", fun egeoip_countrydb/0},
       {"egeoip_lookup", fun egeoip_lookup/0},
       {"egeoip_reserved", {generator, fun egeoip_reserved_gen/0}},
       {"country_test2", {generator, fun country_test2_gen/0}},
       {"non_parallel", fun non_parallel/0}]}}.

egeoip_reserved_gen() ->
    %% We don't test all of them, just a few.
    [?_assertMatch(
        {ok, #geoip{country_code = "",
                    country_code3 = "",
                    country_name = "",
                    region = <<>>,
                    city = <<>>,
                    postal_code = <<>>,
                    area_code = 0,
                    dma_code = 0}},
       egeoip:lookup(Ip))
     || Ip <- ["0.0.0.1", "127.0.0.1", "10.0.0.1", "192.168.0.1"]].

egeoip_bench() ->
    ?assertMatch(
       {_, _},
       egeoip:bench(1)),
    ok.

egeoip() ->
    {ok, IpAddressLong} = egeoip:ip2long({207,145,216,106}),
    {ok, IpAddressLong} = egeoip:ip2long("192.168.3.11"),
    {ok, IpAddressLong} = egeoip:ip2long(<<207,145,216,106>>),
    {ok, R} = egeoip:lookup(IpAddressLong),
    #geoip{country_code = "US",
           country_code3 = "USA",
           country_name = "United States",
           region = <<"CA">>,
           _ = _} = R,
    %% This is the test IP that MaxMind uses
    {ok, R1} = egeoip:lookup("24.24.24.24"),
    #geoip{country_code = "US",
           country_code3 = "USA",
           country_name = "United States",
           region = <<"NY">>,
           _ = _} = R1.


egeoip_fail() ->
    ?assertMatch({ok, #geoip{country_code = "",
                             country_code3 = "",
                             country_name = "",
                             region = <<>>,
                             city = <<>>,
                             postal_code = <<>>,
                             area_code = 0,
                             dma_code = 0}}, egeoip:lookup("2")),
    ok.

%% countrydb doesn't provide region info
egeoip_countrydb() ->
    {ok, IpAddressLong} = egeoip:ip2long({207,145,216,106}),
    {ok, IpAddressLong} = egeoip:ip2long("192.168.3.11"),
    {ok, IpAddressLong} = egeoip:ip2long(<<207,145,216,106>>),
    {ok, R} = egeoip:lookup(IpAddressLong),
    #geoip{country_code = "US",
           country_code3 = "USA",
           country_name = "United States",
           _ = _} = R,
    %% This is the test IP that MaxMind uses
    {ok, R1} = egeoip:lookup("24.24.24.24"),
    #geoip{country_code = "US",
           country_code3 = "USA",
           country_name = "United States",
           _ = _} = R1.

egeoip_lookup() ->
    {ok, R1} = egeoip:lookup("24.24.24.24"),
    {ok, R2} = egeoip:lookup({24,24,24,24}),
    ?assertEqual(R1,R2).

non_parallel() ->
    %% recreate the non-parallelized version of egeoip and then verify
    %% that the upgrade works.
    Workers = [Egeoip | T] = tuple_to_list(egeoip_sup:worker_names()),
    %% Remove all worker processes except for the first one
    lists:map(fun(Worker) ->
                      ok = supervisor:terminate_child(egeoip_sup, Worker),
                      ok = supervisor:delete_child(egeoip_sup, Worker)
              end, T),
    Pid = whereis(Egeoip),
    unregister(Egeoip),
    register(egeoip, Pid),
    ?assert(Pid == whereis(egeoip)),
    [?assert(undefined == whereis(W)) || W <- Workers],
    %% Should upgrade when calling lookup
    {ok, _R} = egeoip:lookup("24.24.24.24"),
    ?assert(undefined == whereis(egeoip)),
    [?assertNot(undefined == whereis(W)) || W <- Workers].

no_egeoip_test() ->
    Lookup = {lookup, "172.16.58.3"},
    ?assertExit({noproc,{gen_server,call,[egeoip,Lookup]}},gen_server:call(egeoip, Lookup)).

country_test_gen() ->
    [{Ip ++ " -> " ++ CC ++ " / " ++ C3,
      ?_assertMatch(
         {ok, #geoip{country_code=CC, country_code3=C3}},
         egeoip:lookup(Ip))}
     || {Ip, CC, C3} <- country_test_data()].

country_test2_gen() ->
    [{Ip ++ " -> " ++ CC,
      ?_assertMatch(
         {ok, #geoip{country_code=CC}},
         egeoip:lookup(Ip))}
     || {Ip, CC} <- country_test2_data()].

country_test2_data() ->
    [{"192.168.127.12", "JO"},
     {"172.16.31.10", "US"},
     {"172.16.17.32", "AU"},
     {"1.2.3.4", "AU"},
     {"172.16.58.3", "SA"},
     {"192.168.3.11", "US"},
     {"172.16.58.3", "SA"},
     {"172.16.31.10", "US"},
     {"172.16.58.3", "US"},
     {"172.16.17.32", "IN"},
     {"192.168.3.11", "CY"},
     {"192.168.3.11", "MX"},
     {"192.168.127.12", "IN"},
     {"192.168.127.12", "PK"},
     {"172.16.17.32", "US"},
     {"192.168.3.11", "UA"},
     {"172.16.17.32", "GB"},
     {"172.16.17.32", "US"},
     {"192.168.3.11", "US"},
     {"172.16.31.10", "BE"},
     {"172.16.17.32", "PL"},
     {"192.168.127.12", "TH"},
     {"172.16.31.10", "US"},
     {"172.16.31.10", "US"},
     {"192.168.3.11", "IN"},
     {"172.16.58.3", "IN"},
     {"172.16.17.32", "ES"},
     {"172.16.31.10", "IL"},
     {"172.16.58.3", "GB"},
     {"192.168.3.11", "NL"}].

country_test_data() ->
    [{"172.16.58.3", "US", "USA"},
     {"192.168.127.12", "IT", "ITA"},
     {"192.168.3.11", "CZ", "CZE"},
     {"172.16.17.32", "JP", "JPN"},
     {"172.16.31.10", "FR", "FRA"},
     {"172.16.17.32", "BR", "BRA"},
     {"192.168.3.11", "DE", "DEU"},
     {"172.16.58.3", "BE", "BEL"},
     {"172.16.17.32", "IT", "ITA"},
     {"172.16.17.32", "AU", "AUS"},
     {"192.168.127.12", "ZA", "ZAF"},
     {"172.16.58.3", "IT", "ITA"},
     {"192.168.127.12", "IT", "ITA"},
     {"172.16.31.10", "FR", "FRA"},
     {"192.168.127.12", "TR", "TUR"},
     {"172.16.31.10", "KR", "KOR"},
     {"172.16.17.32", "KR", "KOR"},
     {"172.16.17.32", "MA", "MAR"},
     {"192.168.127.12", "DE", "DEU"},
     {"192.168.3.11", "DE", "DEU"},
     {"192.168.127.12", "HU", "HUN"},
     {"172.16.31.10", "DE", "DEU"},
     {"192.168.127.12", "IT", "ITA"},
     {"192.168.127.12", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"192.168.3.11", "IT", "ITA"},
     {"172.16.58.3", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"192.168.127.12", "IT", "ITA"},
     {"172.16.58.3", "IT", "ITA"},
     {"172.16.58.3", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"172.16.58.3", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"172.16.17.32", "IT", "ITA"},
     {"192.168.127.12", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.31.10", "IT", "ITA"},
     {"172.16.58.3", "IT", "ITA"},
     {"172.16.31.10", "FR", "FRA"},
     {"192.168.127.12", "TR", "TUR"},
     {"172.16.17.32", "TR", "TUR"},
     {"172.16.31.10", "JP", "JPN"},
     {"172.16.58.3", "JP", "JPN"},
     {"172.16.17.32", "JP", "JPN"},
     {"192.168.127.12", "JP", "JPN"},
     {"172.16.31.10", "KR", "KOR"},
     {"172.16.17.32", "JP", "JPN"},
     {"172.16.31.10", "FR", "FRA"},
     {"172.16.17.32", "BR", "BRA"},
     {"192.168.3.11", "ID", "IDN"},
     {"172.16.31.10", "US", "USA"},
     {"192.168.127.12", "US", "USA"},
     {"172.16.31.10", "US", "USA"},
     {"172.16.31.10", "MY", "MYS"},
     {"172.16.17.32", "AU", "AUS"},
     {"192.168.127.12", "AU", "AUS"},
     {"172.16.31.10", "JP", "JPN"},
     {"172.16.58.3", "SG", "SGP"},
     {"172.16.58.3", "GB", "GBR"},
     {"172.16.17.32", "US", "USA"},
     {"172.16.31.10", "US", "USA"},
     {"172.16.58.3", "US", "USA"}].

{'fraction_non_alphanumeric': 0.26324637320216676, 'fraction_numerical': 0.1379117115995268, 'mean_word_length': 2.2468162522741055, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 222, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7281886', 'n_tokens_mistral': 7928, 'n_tokens_neox': 6560, 'n_words': 1099}