This shows you the differences between two versions of the page.
|
dss:laboratoare:01 [2019/06/17 10:57] razvan.nitu1305 [5. Struct vs. Class] |
dss:laboratoare:01 [2019/08/14 15:07] (current) razvan.nitu1305 [Unittests] |
||
|---|---|---|---|
| Line 148: | Line 148: | ||
| int x = b[1]; // equivalent to `int x = 0;` | int x = b[1]; // equivalent to `int x = 0;` | ||
| a[2] = 3; | a[2] = 3; | ||
| - | int y = b[2]; // equivalent to `int y = 3;` | + | int y = b[1]; // equivalent to `int y = 3;` |
| } | } | ||
| </code> | </code> | ||
| Line 418: | Line 418: | ||
| <code D> | <code D> | ||
| import std.algorithm : group; | import std.algorithm : group; | ||
| - | import std.range : chain, retro, front, retro; | + | import std.range : chain, dropOne, front, retro; |
| [1, 2].chain([3, 4]).retro; // 4, 3, 2, 1 | [1, 2].chain([3, 4]).retro; // 4, 3, 2, 1 | ||
| - | [1, 1, 2, 2, 2].group.dropOne.front; // (2, 3) | + | [1, 1, 2, 2, 2].group.dropOne.front; // (2, 3) |
| + | |||
| + | front(dropOne(group([1, 1, 2, 2, 2]))); | ||
| </code> | </code> | ||
| Line 521: | Line 523: | ||
| <code D> | <code D> | ||
| - | class Sum | + | struct Sum |
| { | { | ||
| int add(int x, int y) { return x + y; } | int add(int x, int y) { return x + y; } | ||
| Line 527: | Line 529: | ||
| unittest | unittest | ||
| { | { | ||
| - | Sum sum = new Sum; | + | Sum sum; |
| assert(sum.add(3,4) == 7); | assert(sum.add(3,4) == 7); | ||
| assert(sum.add(-2,0) == -2); | assert(sum.add(-2,0) == -2); | ||
| Line 604: | Line 606: | ||
| ===== Exercises ===== | ===== Exercises ===== | ||
| + | |||
| + | The exercises for ''lab-01'' are located in this [[https://github.com/RazvanN7/D-Summer-School/tree/master/lab-01|repo]]. | ||
| ==== 1. C to D ==== | ==== 1. C to D ==== | ||
| Line 636: | Line 640: | ||
| - Compile both files and measure the run time of each program. How do you explain the differences? | - Compile both files and measure the run time of each program. How do you explain the differences? | ||
| - In both situations, print the value of the field **a0** after the loop ends. How do you explain the differences? | - In both situations, print the value of the field **a0** after the loop ends. How do you explain the differences? | ||
| + | |||
| + | ==== 6. Voldemort types ==== | ||
| + | |||
| + | Navigate to the ''6-voldemort'' directory. Inspect the source file **voldermort.d**. The declaration of **struct Result** is declared as **private** (nobody has access to it, except the members of the current file). Move the declaration of the **struct Result** inside the **fun** function. Compile the code. Does it compile? Why? Fix the issue. | ||
| + | |||
| + | ==== 7. Sanitization & Unittesting ==== | ||
| + | |||
| + | Navigate to the ''7 - BinarySearch'' directory. Inspect the source file **binarySearch.d**. As the name implies, a binarySearch algorithm is implemented on integers. Compile and run the file. | ||
| + | |||
| + | - Write a unittest function that tests some corner cases. Are there any bugs in the algorithm implementation? If yes, fix them. | ||
| + | - The **binarySearch** function may be called with invalid data (for example: l = -1, r = -2). Write an **in** contract that halts the code execution in the case of invalid input (invalid values for **l** and **r**, the array is not sorted etc.) | ||
| + | - Rewrite the **binarySearch** algorithm to make use of slices. | ||