forked from exercism/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing pangram. (exercism#148)
* Implementing pangram. * completed implementation of pangram. * forgot the config * enforce repo style * further enforced style conventions. * forgot a semicolon. * updated config.json (sorry about that). * changed function input to const reference.
- Loading branch information
Showing
6 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Get the exercise name from the current directory | ||
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME) | ||
|
||
# Basic CMake project | ||
cmake_minimum_required(VERSION 2.8.11) | ||
|
||
# Name the project after the exercise | ||
project(${exercise} CXX) | ||
|
||
# Locate Boost libraries: unit_test_framework, date_time and regex | ||
set(Boost_USE_STATIC_LIBS ON) | ||
set(Boost_USE_MULTITHREADED ON) | ||
set(Boost_USE_STATIC_RUNTIME OFF) | ||
find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework date_time regex) | ||
|
||
# Enable C++11 features on gcc/clang | ||
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)") | ||
set(CMAKE_CXX_FLAGS "-std=c++11") | ||
endif() | ||
|
||
# Configure to run all the tests? | ||
if(${EXERCISM_RUN_ALL_TESTS}) | ||
add_definitions(-DEXERCISM_RUN_ALL_TESTS) | ||
endif() | ||
|
||
# Get a source filename from the exercise name by replacing -'s with _'s | ||
string(REPLACE "-" "_" file ${exercise}) | ||
|
||
# Implementation could be only a header | ||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp) | ||
set(exercise_cpp ${file}.cpp) | ||
else() | ||
set(exercise_cpp "") | ||
endif() | ||
|
||
# Build executable from sources and headers | ||
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h) | ||
|
||
# We need boost includes | ||
target_include_directories(${exercise} PRIVATE ${Boost_INCLUDE_DIRS}) | ||
|
||
# We need boost libraries | ||
target_link_libraries(${exercise} ${Boost_LIBRARIES}) | ||
|
||
# Tell MSVC not to warn us about unchecked iterators in debug builds | ||
if(${MSVC}) | ||
set_target_properties(${exercise} PROPERTIES | ||
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS) | ||
endif() | ||
|
||
# Run the tests on every build | ||
add_custom_command(TARGET ${exercise} POST_BUILD COMMAND ${exercise}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "pangram.h" | ||
#include <cctype> | ||
|
||
namespace | ||
{ | ||
|
||
bool found_letter(char a, const std::string &s) | ||
{ | ||
for(auto c: s) { | ||
if(tolower(c) ==a) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
} | ||
|
||
namespace pangram | ||
{ | ||
|
||
bool is_pangram(const std::string &s) | ||
{ | ||
char letters[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; | ||
for(char l : letters) | ||
{ | ||
if(!found_letter(l, s)) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#if !defined(PANGRAM_H) | ||
#define PANGRAM_H | ||
#include <string> | ||
|
||
namespace pangram | ||
{ | ||
|
||
bool is_pangram(const std::string &s); | ||
|
||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "pangram.h" | ||
#define BOOST_TEST_MAIN | ||
#include <boost/test/unit_test.hpp> | ||
|
||
BOOST_AUTO_TEST_CASE(sentence_empty) | ||
{ | ||
BOOST_REQUIRE(!pangram::is_pangram("")); | ||
} | ||
|
||
#ifdef EXERCISM_RUN_ALL_TESTS | ||
BOOST_AUTO_TEST_CASE(pangram_with_only_lower_case) | ||
{ | ||
BOOST_REQUIRE(pangram::is_pangram("the quick brown fox jumps over the lazy dog")); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(missing_character_x) | ||
{ | ||
BOOST_REQUIRE(!pangram::is_pangram("a quick movement of the enemy will jeopardize five gunboats")); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(another_missing_x) | ||
{ | ||
BOOST_REQUIRE(!pangram::is_pangram("the quick brown fish jumps over the lazy dog")); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(pangram_with_underscores) | ||
{ | ||
BOOST_REQUIRE(pangram::is_pangram("the_quick_brown_fox_jumps_over_the_lazy_dog")); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(pangram_with_numbers) | ||
{ | ||
BOOST_REQUIRE(pangram::is_pangram("the 1 quick brown fox jumps over the 2 lazy dogs")); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(missing_letters_replaced_with_numbers) | ||
{ | ||
BOOST_REQUIRE(!pangram::is_pangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog")); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(pangram_with_mixed_case_and_punctuation) | ||
{ | ||
BOOST_REQUIRE(pangram::is_pangram("\"Five quacking Zephyrs jolt my wax bed.\"")); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(upper_and_lower_should_not_be_counted_seperately) | ||
{ | ||
BOOST_REQUIRE(!pangram::is_pangram("the quick brown fox jumps over with lazy FX")); | ||
} | ||
#endif |