Learn How to Think with Karel the Robot. Chapter 10. Testing Your Programs

Chapter 10
Testing Your Programs

In this chapter you will learn:

  • About the history and applications of the Morse code.
  • How to write your own Morse to English translator.
  • That every new command or function needs to be tested.
  • How to test your new commands and functions.
  • That testing your code thoroughly can save you a lot of grief.

10.1. Morse code project - Part I

The Morse code is a classical communication method which translates standard English characters to short sequences of dots and dashes. It was invented by Samuel Finley Breese Morse (1791 - 1872) with the help of his friend Alfred Vail from New York University.

In 1838, at an exhibition in New York, Morse transmitted 10 words per minute using what would forever be known as Morse code. In 1843, he received money from Congress to build a line from Baltimore to Washington DC, and, on 24 May 1844, he sent the first inter-city message: “What hath God wrought!” By 1854 there were 23,000 miles of telegraph wire in operation across the US. Morse code was later adapted to wireless radio. By the 1930s it was the preferred form of communication for aviators and seamen, and it was vital during the Second World War. BTW, here is the complete Morse code for reference:


Figure 10.1: *

Morse code.

Today, you will teach Karel how to read the Morse code! In the maze below, each row represents one letter. The lollipops stand for dots and the candies for dashes. In other words, the message below represents ’-.-/.-/.-././.-..’ which means ’KAREL’:


Figure 10.2: *

Karel’s Morse code (lollipop = dot, candy = dash).

Writing a Morse code translator is a fairly complex task, therefore let’s decompose it into smaller subtasks. First, Karel needs a function row to read one row (one Morse symbol written with lollipops and candies) and return the corresponding text string of dots and dashes. The function should assume that Karel stands in front of the symbol, facing East. The robot’s final position should be the same, just in the next row down. For example, this maze contains just one letter ’K’:


Therefore the program


should bring Karel to his home square, and display the text string ’-.-’. The corresponding program is straightforward - make sure to read all the comments!

Program 10.1: Read one Morse symbol
1# Read one Morse symbol written in terms of lollipops and candies, 
2# and return it as a text string of dots and dashes: 
3def row 
4  go # move on to the first object 
5  morse =  # create an empty text string 
6  while lollipop or candy # scan the Morse symbol 
7    if lollipop 
8      morse += . # lollipop means dot 
9    else 
10      morse += - # candy means dash 
11    go 
12  repeat 2 # turn around at the end 
13    right 
14  n = len(morse) # figure out how many steps you made 
15  repeat n+1 
16    go 
17  left # move one row to the South 
18  go 
19  left 
20  return morse # return the resulting text string 
22# Main program: 

And indeed, when the program is executed with the above maze, Karel enters his home square and the correct symbol ’-.-’ is displayed:


Well, that’s great, but it only proves that the function row works correctly for the letter ’K’. What about the remaining 25 letters?

10.2. Testing the function row

Testing your software costs time, and sometimes it can be lengthy. But it is time very well spent. Therefore, let’s stop writing new programs for a while, and prepare three mazes which will cover all 26 letters of the English alphabet:


Below is a simple testing program which will read all rows in the maze and display the corresponding Morse codes in terms of dots and dashes:

Program 10.2: Simple program to test the function row
1while not home 
2  print(row)

Let’s run it with the three testing mazes above. And indeed, all three outputs are correct!


Software companies, depending on their size, use software testers or entire testing departments. There is a famous quote which says "The longer you wait to fix a problem, the more expensive and troublesome it will be to fix." Therefore, the best option is to prevent problems from happening in the first place. And we just did that - we made sure that there won’t be any problems with our function row. This is golden. picture

10.3. Morse code project - Part II

Awesome! Now we have a function row which turns Karel maze symbols into regular Morse code text strings. Next, we need a function to translate Morse text strings to English letters. This is straightforward, although lengthy because we have to account for all 26 English characters. The following program includes the function as well as a main program which should display the letter ’K’:

Program 10.3: Translate individual Morse symbols to English
1# Translate one Morse symbol to an English letter: 
2def english(symbol) 
3  if symbol == .- 
4    return A 
5  elif symbol == -... 
6    return B 
7  elif symbol == -.-. 
8    return C 
9  elif symbol == -.. 
10    return D 
11  elif symbol == . 
12    return E 
13  elif symbol == ..-. 
14    return F 
15  elif symbol == --. 
16    return G 
17  elif symbol == .... 
18    return H 
19  elif symbol == .. 
20    return I 
21  elif symbol == .--- 
22    return J 
23  elif symbol == -.- 
24    return K 
25  elif symbol == .-.. 
26    return L 
27  elif symbol == -- 
28    return M 
29  elif symbol == -. 
30    return N 
31  elif symbol == --- 
32    return O 
33  elif symbol == .--. 
34    return P 
35  elif symbol == --.- 
36    return Q 
37  elif symbol == .-. 
38    return R 
39  elif symbol == ... 
40    return S 
41  elif symbol == - 
42    return T 
43  elif symbol == ..- 
44    return U 
45  elif symbol == ...- 
46    return V 
47  elif symbol == .-- 
48    return W 
49  elif symbol == -..- 
50    return X 
51  elif symbol == -.-- 
52    return Y 
53  elif symbol == --.. 
54    return Z 
55  else 
56    print(~Wrong Morse symbol detected in english() - returning *.~) 
57    return * 
59# Main program: 
60s = -.- 

Notice that if the function encounters an unknown symbol, it will return ’*’. This ’*’ will then appear in the resulting English text string, so it will be easy to spot. OK, let’s execute the program now!


This is the expected output - great! But what about the remaining 25 letters?

10.4. Testing the function english(symbol)

It is important to call the function with all 26 Morse symbols ’.-’, ’-...’, ’-.-.’, ..., ’—..’ and verify that indeed we obtain the corresponding English letters ’A’, ’B’, ’C’, ..., ’Z’:

Program 10.4: Program to test the function english()
1# Testing the function english(): 
2r =  
3s = .- 
4r += english(s) 
5s = -... 
6r += english(s) 
7s = -.-. 
8r += english(s) 
9s = -.. 
10r += english(s) 
11s = . 
12r += english(s) 
13s = ..-. 
14r += english(s) 
15s = --. 
16r += english(s) 
17s = .... 
18r += english(s) 
19s = .. 
20r += english(s) 
21s = .--- 
22r += english(s) 
23s = -.- 
24r += english(s) 
25s = .-.. 
26r += english(s) 
27s = -- 
28r += english(s) 
29s = -. 
30r += english(s) 
31s = --- 
32r += english(s) 
33s = .--. 
34r += english(s) 
35s = --.- 
36r += english(s) 
37s = .-. 
38r += english(s) 
39s = ... 
40r += english(s) 
41s = - 
42r += english(s) 
43s = ..- 
44r += english(s) 
45s = ...- 
46r += english(s) 
47s = .-- 
48r += english(s) 
49s = -..- 
50r += english(s) 
51s = -.-- 
52r += english(s) 
53s = --.. 
54r += english(s) 

And here is the corresponding output,


which shows that the function works correctly. picture

10.5. Morse code project - Part III

Super, we are ready to finish! With the heavy lifting done by the functions row and english (symbol), the main function morse2english is very easy to write:

Program 10.5: Putting all parts together
1# Read Morse symbols in Karel maze, and translate them to English text: 
2def morse2english 
3  text =  
4  while not home 
5    text += english(row) 
6  return text 
8# Main program: 

The final program will comprise the functions row, english(symbol), and the last code above. Now let’s run it with the original maze,




Indeed, this result is correct! The function morse2english is so simple that it does not require testing. Importantly, it works for the sample maze above, and its two essential parts, the functions row and english(symbol), were thoroughly tested.

10.6. Your homework

Here is something for you to think about: The function row does not consider empty spaces between words. Would you be able to adjust it to return an empty space ’ ’ if it encounters an empty row in the maze?

And as a second task - implement your own English to Morse translator! It’s OK to stay on the text level, you don’t need to create a graphical representation of the Morse symbols in the maze.

10.7. Review questions

Friendly reminder - for every question either none, one, or several answers may be correct.

QUESTION 10.1. Is it mandatory that software developers test their software?


QUESTION 10.2. Is it recommended that software developers test their software?


QUESTION 10.3. What happens if a problem in the software is not fixed for a long time?

It disappears by itself.
Everybody forgets about it.
It will be very expensive and time consuming to fix.
It may cause users to get upset and lose interest in the software.

QUESTION 10.4. Is software testing supposed to be a quick and easy process?

Yes, otherwise nobody would be doing it.
Yes, because the testing does not have to be thorough.
Yes, because someone else will do it.
No, testing software can take a lot of time.

QUESTION 10.5. Should each new command or function be isolated from the rest of the code for testing?

No, that would just be more work.
No, testing a new command or function within the original code is better.
No, because the new command or function might not work when put back.
Yes, to avoid the influence of potential bugs elsewhere in the code.

Table of Contents

Created on September 27, 2018 in Karel.
Add Comment
0 Comment(s)

Your Comment

By posting your comment, you agree to the privacy policy and terms of service.