diff --git a/exercises/practice/high-scores/.meta/example.R b/exercises/practice/high-scores/.meta/example.R index e69de29b..6eebcf29 100644 --- a/exercises/practice/high-scores/.meta/example.R +++ b/exercises/practice/high-scores/.meta/example.R @@ -0,0 +1,15 @@ +scores_list <- function(scores) { + scores +} + +latest <- function(scores) { + tail(scores, n = 1) +} + +personal_best <- function(scores) { + max(scores) +} + +personal_top_three <- function(scores) { + sort(scores, decreasing = TRUE)[1:(min(length(scores), 3))] +} diff --git a/exercises/practice/high-scores/high-scores.R b/exercises/practice/high-scores/high-scores.R index e69de29b..debddeac 100644 --- a/exercises/practice/high-scores/high-scores.R +++ b/exercises/practice/high-scores/high-scores.R @@ -0,0 +1,15 @@ +scores_list <- function(scores) { + +} + +latest <- function(scores) { + +} + +personal_best <- function(scores) { + +} + +personal_top_three <- function(scores) { + +} diff --git a/exercises/practice/high-scores/test_high-scores.R b/exercises/practice/high-scores/test_high-scores.R index 5c3dac09..9e3f2286 100644 --- a/exercises/practice/high-scores/test_high-scores.R +++ b/exercises/practice/high-scores/test_high-scores.R @@ -1,69 +1,78 @@ +source("./high-scores.R") +library(testthat) +test_that("list of scores", { + scores <- c(30, 50, 20, 70) + expected <- c(30, 50, 20, 70) + expect_equal(scores_list(scores), expected) +}) -def test_list_of_scores(self): - scores = [30, 50, 20, 70] - expected = [30, 50, 20, 70] - self.assertEqual(HighScores(scores).scores, expected) +test_that("latest score", { + scores <- c(100, 0, 90, 30) + expected <- 30 + expect_equal(latest(scores), expected) +}) - def test_latest_score(self): - scores = [100, 0, 90, 30] - expected = 30 - self.assertEqual(HighScores(scores).latest(), expected) +test_that("personal best", { + scores <- c(40, 100, 70) + expected <- 100 + expect_equal(personal_best(scores), expected) +}) - def test_personal_best(self): - scores = [40, 100, 70] - expected = 100 - self.assertEqual(HighScores(scores).personal_best(), expected) +test_that("personal top three from a list of scores", { + scores <- c(10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70) + expected <- c(100, 90, 70) + expect_equal(personal_top_three(scores), expected) +}) - def test_personal_top_three_from_a_list_of_scores(self): - scores = [10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70] - expected = [100, 90, 70] - self.assertEqual(HighScores(scores).personal_top_three(), expected) +test_that("personal top highest to lowest", { + scores <- c(20, 10, 30) + expected <- c(30, 20, 10) + expect_equal(personal_top_three(scores), expected) +}) - def test_personal_top_highest_to_lowest(self): - scores = [20, 10, 30] - expected = [30, 20, 10] - self.assertEqual(HighScores(scores).personal_top_three(), expected) +test_that("personal top when there is a tie", { + scores <- c(40, 20, 40, 30) + expected <- c(40, 40, 30) + expect_equal(personal_top_three(scores), expected) +}) - def test_personal_top_when_there_is_a_tie(self): - scores = [40, 20, 40, 30] - expected = [40, 40, 30] - self.assertEqual(HighScores(scores).personal_top_three(), expected) +test_that("personal top when there are less than 3", { + scores <- c(30, 70) + expected <- c(70, 30) + expect_equal(personal_top_three(scores), expected) +}) - def test_personal_top_when_there_are_less_than_3(self): - scores = [30, 70] - expected = [70, 30] - self.assertEqual(HighScores(scores).personal_top_three(), expected) +test_that("personal top when there is only one", { + scores <- c(40) + expected <- c(40) + expect_equal(personal_top_three(scores), expected) +}) - def test_personal_top_when_there_is_only_one(self): - scores = [40] - expected = [40] - self.assertEqual(HighScores(scores).personal_top_three(), expected) +test_that("latest score after personal top scores", { + scores <- c(70, 50, 20, 30) + expected <- 30 + personal_top_three(scores) + expect_equal(latest(scores), expected) +}) - def test_latest_score_after_personal_top_scores(self): - scores = [70, 50, 20, 30] - expected = 30 - highscores = HighScores(scores) - highscores.personal_top_three() - self.assertEqual(highscores.latest(), expected) +test_that("scores after personal top scores", { + scores <- c(30, 50, 20, 70) + expected <- c(30, 50, 20, 70) + personal_top_three(scores) + expect_equal(scores_list(scores), expected) +}) - def test_scores_after_personal_top_scores(self): - scores = [30, 50, 20, 70] - expected = [30, 50, 20, 70] - highscores = HighScores(scores) - highscores.personal_top_three() - self.assertEqual(highscores.scores, expected) +test_that("latest score after personal best", { + scores <- c(20, 70, 15, 25, 30) + expected <- 30 + personal_best(scores) + expect_equal(latest(scores), expected) +}) - def test_latest_score_after_personal_best(self): - scores = [20, 70, 15, 25, 30] - expected = 30 - highscores = HighScores(scores) - highscores.personal_best() - self.assertEqual(highscores.latest(), expected) - - def test_scores_after_personal_best(self): - scores = [20, 70, 15, 25, 30] - expected = [20, 70, 15, 25, 30] - highscores = HighScores(scores) - highscores.personal_best() - self.assertEqual(highscores.scores, expected) \ No newline at end of file +test_that("scores after personal best", { + scores <- c(20, 70, 15, 25, 30) + expected <- c(20, 70, 15, 25, 30) + personal_best(scores) + expect_equal(scores_list(scores), expected) +})