Measure Twice
The first “commit” (code contribution) to the stringr package.
Problem to Solve
The str_length
function in the stringr package measures the length of strings. For example, str_length("abc")
returns 3, the number of characters in the string.
Of course, the str_length
function was written by a programmer like you! Someone, at some point, sat down to create a function that easily measures the length of strings. To be sure the function would work correctly for you, too, they wrote some unit tests to methodically check their work.
In this problem, you’ll build on the work of str_length
’s author by writing tests that ensure the function behaves as expected. In a file called test-str_length.R
, in a folder called measure
, write a set of test cases to thoroughly test the str_length
function.
Getting Started
For this problem, you’ll need to create test-str_length.R
in a folder called measure
.
Create test-str_length.R
Open RStudio per the linked steps and navigate to the R console:
>
Next execute
getwd()
to print your working directory. Ensure your current working directory is where you’d like to create this problem’s folder. If using RStudio through cs50.dev the recommended directory is /workspaces/NUMBER
where NUMBER
is a number unique to your codespace.
If you do not see the right working directory, use setwd
to change it! Try typing setwd("..")
if in the working directory of another problem, which will move you one directory higher.
Next execute
dir.create("measure")
in order to create a folder called measure
in your codespace.
Now type
setwd("measure")
followed by Enter to move yourself into (i.e., open) that directory. Your working directory should now end with
measure/
Finally, type
file.create("test-str_length.R")
to create a file called test-str_length.R
inside of the measure
folder.
If all was successful, you should execute
list.files()
and see test-str_length.R
. If not, retrace your steps and see if you can determine where you went wrong!
Specification
In test-str_length.R
, write a series of testthat tests to thoroughly test the str_length
function. As inspiration, consider taking a look at the (actual!) tests for str_length
at github.com/tidyverse/stringr/blob/main/tests/testthat/test-length.R. Odds are you should be able to understand most of these tests as written!
You’ll want to begin test-str_length.R
by loading both the stringr package and testthat:
library(stringr)
library(testthat)
Below, write at least four tests with at least one test case each. That is, be sure to invoke test_that
and an expect
function at least four times. Your tests should expect the behavior outlined in the documentation for str_length
, stringr.tidyverse.org/reference/str_length.html.
Advice
Unsure what to test? Consider any of the below:
- Does
str_length
return the expected output for non-alphabetical characters, such as whitespace, punctuation, or emoji? - Does
str_length
return the expected output for special values such asNA
,NaN
,Inf
, and-Inf
? - Does
str_length
return the expected output for vector inputs? What about a data frame as input?
Usage
Assuming test-str_length.R
is in your working directory, enter the below in the R console to run your program:
source("test-str_length.R")
How to Test
check50
You can check your code using check50
, a program that CS50 will use to test your code when you submit. But be sure to test it yourself as well!
Run the following command in the RStudio console:
check50("cs50/problems/2024/r/measure")
Green smilies mean your program has passed a test! Red frownies will indicate your program output something unexpected. Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.
How to Submit
You can submit your code using submit50
.
Keeping in mind the course’s policy on academic honesty, run the following command in the RStudio console:
submit50("cs50/problems/2024/r/measure")