github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/assignments/testdata/tests/lab3/task-learn-go.md (about)

     1  # Go Exercises
     2  
     3  Before you start working on the assignments below, make sure that your local working copy has all the latest changes from the course [assignments](https://github.com/COURSE_TAG/assignments) repository.
     4  Instructions for fetching the latest changes are [here](https://github.com/COURSE_TAG/info/blob/main/lab-submission.md#update-local-working-copy-from-course-assignments).
     5  
     6  1. In the following, we will use `sequence/triangular.go` exercise as an example.
     7     The file contains the following skeleton code and task description:
     8  
     9      ```golang
    10      package sequence
    11  
    12      // Task 1: Triangular numbers
    13      //
    14      // triangular(n) returns the n-th Triangular number, and is defined by the
    15      // recurrence relation F_n = n + F_n-1 where F_0=0 and F_1=1
    16      //
    17      // Visualization of numbers:
    18      // n = 1:    n = 2:     n = 3:      n = 4:    etc...
    19      //   o         o          o           o
    20      //            o o        o o         o o
    21      //                      o o o       o o o
    22      //                                 o o o o
    23      func triangular(n uint) uint {
    24          return 0
    25      }
    26      ```
    27  
    28  2. Implement the function body according to the specification so that all the tests in `sequence/triangular_test.go` passes.
    29     The test file looks like this:
    30  
    31      ```golang
    32      package sequence
    33  
    34      import (
    35          "testing"
    36  
    37          "github.com/google/go-cmp/cmp"
    38      )
    39  
    40      var triangularTests = []struct {
    41          in, want uint
    42      }{
    43          {0, 0},
    44          {1, 1},
    45          {2, 3},
    46          {3, 6},
    47          {4, 10},
    48          {5, 15},
    49          {6, 21},
    50          {7, 28},
    51          {8, 36},
    52          {9, 45},
    53          {10, 55},
    54          {20, 210},
    55      }
    56  
    57      func TestTriangular(t *testing.T) {
    58          for _, test := range triangularTests {
    59              if diff := cmp.Diff(test.want, triangular(test.in)); diff != "" {
    60                  t.Errorf("triangular(%d): (-want +got):\n%s", test.in, diff)
    61              }
    62          }
    63      }
    64      ```
    65  
    66  3. There are several ways to run the tests. If you run:
    67  
    68     ```console
    69     go test
    70     ```
    71  
    72     the Go tool will run all tests found in files whose file name ends with `_test.go` (in the current directory).
    73     Similarly, you can also run a specific test as follows:
    74  
    75     ```console
    76     go test -run TestTriangular
    77     ```
    78  
    79  4. You should ***not*** edit files or code that are marked with a `// DO NOT EDIT` comment.
    80     Please make separate `filename_test.go` files if you wish to write and run your own tests.
    81  
    82  5. When you have completed a task and sufficiently many local tests pass, you may push your code to GitHub.
    83     This will trigger QuickFeed which will then run a separate test suite on your code.
    84  
    85     Using `sequence/triangular.go` as an example, use the following procedure to commit and push your changes to GitHub and QuickFeed:
    86  
    87      ```console
    88      $ git add triangular.go
    89      $ git commit
    90      // This will open an editor for you to write a commit message
    91      // Use for example "Implemented Assignment 2"
    92      $ git push
    93      ```
    94  
    95  6. QuickFeed will now build and run a test suite on the code you submitted.
    96     You can check the output by going to the [QuickFeed web interface](https://uis.itest.run).
    97     The results (build log) is available from the Labs menu.
    98     Note that the results shows output for all the tests in current lab assignment.
    99     You will want to focus on the output for the specific test results related to the task you're working on.
   100  
   101  7. Follow the same process for the other tasks included in this lab assignment.
   102     Each task contains a single `.go` template file, along with a task description and a `_test.go` file with tests.
   103  
   104  8. Finally, complete the task in `cmd/terminal/main.go`.
   105  
   106     *Note: You must print the current working directory at the start of every prompt.*
   107     For example, if the working directory is `/home/username`, your prompt should be something like the following:
   108  
   109     ```console
   110     /home/username>
   111     ```
   112  
   113     Then we can input commands after the prompt, e.g. the `ls` command:
   114  
   115     ```console
   116     /home/username> ls
   117     ```
   118  
   119     You can use the [`os.Getwd` function](https://golang.org/pkg/os/#Getwd) to find the current working directory.
   120     This could be implemented in a function named `printPrompt()`.
   121  
   122  9. When you are done with all assignments and want to submit the final version, please follow these [instructions](https://github.com/COURSE_TAG/info/blob/main/lab-submission.md#final-submission-of-labx).