Skip to content

Commit

Permalink
feat: added continents function #984 (#985)
Browse files Browse the repository at this point in the history
* Update location_data.h

added data for continents

* Update location.cpp

added module for continents

* Update location_test.cpp

* Update location.cpp

* Update location.cpp

* Update location.h

* Update location_data.h

* Update location_data.h

* feat: added continents function #984

* removed unwatned

* removed unused methods

* removed comments and empty spaces, also added feature for random continent

* making sure it uses predefined list of continents for random selection

* added comments and removed iostream

* Update location.h

added comment africa
  • Loading branch information
charan-003 authored Nov 19, 2024
1 parent 58d922c commit f0b97e6
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 2 deletions.
16 changes: 16 additions & 0 deletions include/faker-cxx/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
28 changes: 26 additions & 2 deletions src/modules/location.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include "faker-cxx/location.h"


#include <string>
#include <string_view>
#include <unordered_map>

#include <unordered_set>
#include <stdexcept>
#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"

Expand Down Expand Up @@ -88,6 +90,28 @@ CountryAddressesInfo getAddresses(const Locale& locale)
}
}

std::string_view continent(std::string_view country)
{
static const std::vector<std::string_view> 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);
Expand Down
259 changes: 259 additions & 0 deletions src/modules/location_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,265 @@ struct CountryAddressesInfo
std::span<const std::string_view> states;
};

const auto allContinents = std::to_array<std::string_view>({
"Africa",
"Antarctica",
"Asia",
"Europe",
"North America",
"Australia",
"South America"
});

const std::unordered_map<std::string_view, std::string_view> 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<std::string_view>({
// clang-format off
"Afghanistan",
Expand Down
24 changes: 24 additions & 0 deletions tests/modules/location_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}));
}

0 comments on commit f0b97e6

Please sign in to comment.