diff --git a/include/faker-cxx/location.h b/include/faker-cxx/location.h index a2cade67..8d2f3218 100644 --- a/include/faker-cxx/location.h +++ b/include/faker-cxx/location.h @@ -21,6 +21,22 @@ namespace faker::location */ FAKER_CXX_EXPORT std::string_view country(); +/** + * @brief Returns the continent for a given country or a random continent if no country is specified. + * + * @param country The country name. Defaults to an empty string, which means a random continent is selected. + * + * @returns Continent name or "Unknown" if the country is not mapped. + * + * @code + * faker::location::continent("Poland") // Europe + * faker::location::continent() // Africa + * + * @endcode + */ +FAKER_CXX_EXPORT std::string_view continent(std::string_view country = ""); + + /** * @brief Returns a random country code. * diff --git a/src/modules/location.cpp b/src/modules/location.cpp index 8b535b88..ec442e84 100644 --- a/src/modules/location.cpp +++ b/src/modules/location.cpp @@ -1,15 +1,17 @@ #include "faker-cxx/location.h" + #include #include #include - +#include +#include +#include "faker-cxx/types/locale.h" #include "common/algo_helper.h" #include "common/format_helper.h" #include "faker-cxx/helper.h" #include "faker-cxx/number.h" #include "faker-cxx/person.h" -#include "faker-cxx/types/locale.h" #include "faker-cxx/types/precision.h" #include "location_data.h" @@ -88,6 +90,28 @@ CountryAddressesInfo getAddresses(const Locale& locale) } } +std::string_view continent(std::string_view country) +{ + static const std::vector continents = { + "Africa", "Antarctica", "Asia", "Europe", "North America", "Australia", "South America"}; + + if (country.empty()) + { + return helper::randomElement(continents); + } + + auto it = countryToContinent.find(country); + if (it != countryToContinent.end()) + { + return it->second; + } + + return "Unknown"; +} + + + + std::string_view country() { return helper::randomElement(allCountries); diff --git a/src/modules/location_data.h b/src/modules/location_data.h index a01403f1..9e262171 100644 --- a/src/modules/location_data.h +++ b/src/modules/location_data.h @@ -23,6 +23,265 @@ struct CountryAddressesInfo std::span states; }; +const auto allContinents = std::to_array({ + "Africa", + "Antarctica", + "Asia", + "Europe", + "North America", + "Australia", + "South America" +}); + +const std::unordered_map countryToContinent = { +{"Afghanistan", "Asia"}, + {"Aland Islands", "Europe"}, + {"Albania", "Europe"}, + {"Algeria", "Africa"}, + {"American Samoa", "Australia"}, + {"Andorra", "Europe"}, + {"Angola", "Africa"}, + {"Anguilla", "North America"}, + {"Antarctica", "Antarctica"}, + {"Antigua and Barbuda", "North America"}, + {"Argentina", "South America"}, + {"Armenia", "Asia"}, + {"Aruba", "North America"}, + {"Australia", "Australia"}, + {"Austria", "Europe"}, + {"Azerbaijan", "Asia"}, + {"Bahamas", "North America"}, + {"Bahrain", "Asia"}, + {"Bangladesh", "Asia"}, + {"Barbados", "North America"}, + {"Belarus", "Europe"}, + {"Belgium", "Europe"}, + {"Belize", "North America"}, + {"Benin", "Africa"}, + {"Bermuda", "North America"}, + {"Bhutan", "Asia"}, + {"Bolivia", "South America"}, + {"Bonaire Sint Eustatius and Saba", "North America"}, + {"Bosnia and Herzegovina", "Europe"}, + {"Botswana", "Africa"}, + {"Bouvet Island", "Antarctica"}, + {"Brazil", "South America"}, + {"British Indian Ocean Territory (Chagos Archipelago)", "Asia"}, + {"Brunei Darussalam", "Asia"}, + {"Bulgaria", "Europe"}, + {"Burkina Faso", "Africa"}, + {"Burundi", "Africa"}, + {"Cambodia", "Asia"}, + {"Cameroon", "Africa"}, + {"Canada", "North America"}, + {"Cape Verde", "Africa"}, + {"Cayman Islands", "North America"}, + {"Central African Republic", "Africa"}, + {"Chad", "Africa"}, + {"Chile", "South America"}, + {"China", "Asia"}, + {"Christmas Island", "Asia"}, + {"Cocos (Keeling) Islands", "Asia"}, + {"Colombia", "South America"}, + {"Comoros", "Africa"}, + {"Congo", "Africa"}, + {"Cook Islands", "Australia"}, + {"Costa Rica", "North America"}, + {"Croatia", "Europe"}, + {"Cuba", "North America"}, + {"Curacao", "North America"}, + {"Cyprus", "Asia"}, + {"Czechia", "Europe"}, + {"Democratic Republic of the Congo", "Africa"}, + {"Denmark", "Europe"}, + {"Djibouti", "Africa"}, + {"Dominica", "North America"}, + {"Dominican Republic", "North America"}, + {"Ecuador", "South America"}, + {"Egypt", "Africa"}, + {"El Salvador", "North America"}, + {"Equatorial Guinea", "Africa"}, + {"Eritrea", "Africa"}, + {"Estonia", "Europe"}, + {"Eswatini", "Africa"}, + {"Ethiopia", "Africa"}, + {"Falkland Islands (Malvinas)", "South America"}, + {"Faroe Islands", "Europe"}, + {"Fiji", "Australia"}, + {"Finland", "Europe"}, + {"France", "Europe"}, + {"French Guiana", "South America"}, + {"French Polynesia", "Australia"}, + {"French Southern Territories", "Antarctica"}, + {"Gabon", "Africa"}, + {"Gambia", "Africa"}, + {"Georgia", "Asia"}, + {"Germany", "Europe"}, + {"Ghana", "Africa"}, + {"Gibraltar", "Europe"}, + {"Greece", "Europe"}, + {"Greenland", "North America"}, + {"Grenada", "North America"}, + {"Guadeloupe", "North America"}, + {"Guam", "Australia"}, + {"Guatemala", "North America"}, + {"Guernsey", "Europe"}, + {"Guinea", "Africa"}, + {"Guinea-Bissau", "Africa"}, + {"Guyana", "South America"}, + {"Haiti", "North America"}, + {"Heard Island and McDonald Islands", "Antarctica"}, + {"Holy See (Vatican City State)", "Europe"}, + {"Honduras", "North America"}, + {"Hong Kong", "Asia"}, + {"Hungary", "Europe"}, + {"Iceland", "Europe"}, + {"India", "Asia"}, + {"Indonesia", "Asia"}, + {"Iran", "Asia"}, + {"Iraq", "Asia"}, + {"Ireland", "Europe"}, + {"Isle of Man", "Europe"}, + {"Israel", "Asia"}, + {"Italy", "Europe"}, + {"Jamaica", "North America"}, + {"Japan", "Asia"}, + {"Jersey", "Europe"}, + {"Jordan", "Asia"}, + {"Kazakhstan", "Asia"}, + {"Kenya", "Africa"}, + {"Kiribati", "Australia"}, + {"Kuwait", "Asia"}, + {"Kyrgyz Republic", "Asia"}, + {"Latvia", "Europe"}, + {"Lebanon", "Asia"}, + {"Lesotho", "Africa"}, + {"Liberia", "Africa"}, + {"Libyan Arab Jamahiriya", "Africa"}, + {"Liechtenstein", "Europe"}, + {"Lithuania", "Europe"}, + {"Luxembourg", "Europe"}, + {"Macao", "Asia"}, + {"Madagascar", "Africa"}, + {"Malawi", "Africa"}, + {"Malaysia", "Asia"}, + {"Maldives", "Asia"}, + {"Mali", "Africa"}, + {"Malta", "Europe"}, + {"Marshall Islands", "Australia"}, + {"Martinique", "North America"}, + {"Mauritania", "Africa"}, + {"Mauritius", "Africa"}, + {"Mayotte", "Africa"}, + {"Mexico", "North America"}, + {"Micronesia", "Australia"}, + {"Moldova", "Europe"}, + {"Monaco", "Europe"}, + {"Mongolia", "Asia"}, + {"Montenegro", "Europe"}, + {"Montserrat", "North America"}, + {"Morocco", "Africa"}, + {"Mozambique", "Africa"}, + {"Myanmar", "Asia"}, + {"Namibia", "Africa"}, + {"Nauru", "Australia"}, + {"Nepal", "Asia"}, + {"Netherlands", "Europe"}, + {"New Caledonia", "Australia"}, + {"New Zealand", "Australia"}, + {"Nicaragua", "North America"}, + {"Niger", "Africa"}, + {"Nigeria", "Africa"}, + {"Niue", "Australia"}, + {"Norfolk Island", "Australia"}, + {"North Macedonia", "Europe"}, + {"Northern Mariana Islands", "Australia"}, + {"Norway", "Europe"}, + {"Oman", "Asia"}, + {"Pakistan", "Asia"}, + {"Palau", "Australia"}, + {"Palestine", "Asia"}, + {"Panama", "North America"}, + {"Papua New Guinea", "Australia"}, + {"Paraguay", "South America"}, + {"Peru", "South America"}, + {"Philippines", "Asia"}, + {"Pitcairn Islands", "Australia"}, + {"Poland", "Europe"}, + {"Portugal", "Europe"}, + {"Puerto Rico", "North America"}, + {"Qatar", "Asia"}, + {"Republic of Korea", "Asia"}, + {"Reunion", "Africa"}, + {"Romania", "Europe"}, + {"Russian Federation", "Europe/Asia"}, + {"Rwanda", "Africa"}, + {"Saint Barthelemy", "North America"}, + {"Saint Helena", "Africa"}, + {"Saint Kitts and Nevis", "North America"}, + {"Saint Lucia", "North America"}, + {"Saint Martin", "North America"}, + {"Saint Pierre and Miquelon", "North America"}, + {"Saint Vincent and the Grenadines", "North America"}, + {"Samoa", "Australia"}, + {"San Marino", "Europe"}, + {"Sao Tome and Principe", "Africa"}, + {"Saudi Arabia", "Asia"}, + {"Senegal", "Africa"}, + {"Serbia", "Europe"}, + {"Seychelles", "Africa"}, + {"Sierra Leone", "Africa"}, + {"Singapore", "Asia"}, + {"Sint Maarten", "North America"}, + {"Slovakia", "Europe"}, + {"Slovenia", "Europe"}, + {"Solomon Islands", "Australia"}, + {"Somalia", "Africa"}, + {"South Africa", "Africa"}, + {"South Georgia and the South Sandwich Islands", "Antarctica"}, + {"South Sudan", "Africa"}, + {"Spain", "Europe"}, + {"Sri Lanka", "Asia"}, + {"Sudan", "Africa"}, + {"Suriname", "South America"}, + {"Svalbard & Jan Mayen Islands", "Europe"}, + {"Sweden", "Europe"}, + {"Switzerland", "Europe"}, + {"Syrian Arab Republic", "Asia"}, + {"Taiwan", "Asia"}, + {"Tajikistan", "Asia"}, + {"Tanzania", "Africa"}, + {"Thailand", "Asia"}, + {"Timor-Leste", "Asia"}, + {"Togo", "Africa"}, + {"Tokelau", "Australia"}, + {"Tonga", "Australia"}, + {"Trinidad and Tobago", "North America"}, + {"Tunisia", "Africa"}, + {"Turkey", "Asia/Europe"}, + {"Turkmenistan", "Asia"}, + {"Turks and Caicos Islands", "North America"}, + {"Tuvalu", "Australia"}, + {"Uganda", "Africa"}, + {"Ukraine", "Europe"}, + {"United Arab Emirates", "Asia"}, + {"United Kingdom", "Europe"}, + {"United States Minor Outlying Islands", "Australia"}, + {"United States of America", "North America"}, + {"Uruguay", "South America"}, + {"Uzbekistan", "Asia"}, + {"Vanuatu", "Australia"}, + {"Venezuela", "South America"}, + {"Vietnam", "Asia"}, + {"Virgin Islands British", "North America"}, + {"Virgin Islands U.S.", "North America"}, + {"Wallis and Futuna", "Australia"}, + {"Western Sahara", "Africa"}, + {"Yemen", "Asia"}, + {"Zambia", "Africa"}, + {"Zimbabwe", "Africa"} +}; + const auto allCountries = std::to_array({ // clang-format off "Afghanistan", diff --git a/tests/modules/location_test.cpp b/tests/modules/location_test.cpp index 96e32b3c..a8ae9b35 100644 --- a/tests/modules/location_test.cpp +++ b/tests/modules/location_test.cpp @@ -12,6 +12,8 @@ #include "location_data.h" #include "person_data.h" #include "string_data.h" +#include "faker-cxx/location.h" +#include "location_data.h" #ifndef M_PI #define M_PI 3.14159265358979323846 @@ -1139,4 +1141,26 @@ TEST_F(LocationTest, shouldGeneratepalestineStreetAddress) } +class LocationContinentTest : public ::testing::Test {}; + +TEST_F(LocationContinentTest, shouldGenerateCorrectContinentForKnownCountry) +{ + ASSERT_EQ(continent("Afghanistan"), "Asia"); + ASSERT_EQ(continent("Algeria"), "Africa"); + ASSERT_EQ(continent("Australia"), "Australia"); + ASSERT_EQ(continent("Antarctica"), "Antarctica"); +} + +TEST_F(LocationContinentTest, shouldReturnUnknownForUnmappedCountry) +{ + ASSERT_EQ(continent("Atlantis"), "Unknown"); +} + +TEST_F(LocationContinentTest, shouldGenerateRandomContinent) +{ + const auto generatedContinent = continent(); + ASSERT_TRUE(std::ranges::any_of(allContinents, [&generatedContinent](const std::string_view& c) { + return c == generatedContinent; + })); +}