Differences

This shows you the differences between two versions of the page.

Link to this comparison view

dss:laboratoare:01 [2019/06/08 11:09]
razvan.nitu1305
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 518: Line 520:
 </​code>​ </​code>​
  
-Individual tests are specified in the unit test using [[https://​dlang.org/​spec/​expression.html#​AssertExpression | assert expressions]. There can be any number of unit test functions in a module, including within struct and class declarations. They are executed in lexical order. Unit tests, when enabled, are run after all static initialization is complete and before the **main()** function is called.+Individual tests are specified in the unit test using [[https://​dlang.org/​spec/​expression.html#​AssertExpression | assert expressions]]. There can be any number of unit test functions in a module, including within struct and class declarations. They are executed in lexical order. Unit tests, when enabled, are run after all static initialization is complete and before the **main()** function is called.
  
 <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 537: Line 539:
  
 Contracts are a breakthrough technique to reduce the programming effort for large projects. Contracts are the concept of preconditions,​ postconditions,​ errors, and invariants. Contracts are a breakthrough technique to reduce the programming effort for large projects. Contracts are the concept of preconditions,​ postconditions,​ errors, and invariants.
 +
 +=== Preconditions/​Postconditions ===
 +
 +The pre contracts specify the preconditions before a statement is executed. The most typical use of this would be in validating the parameters to a function. The post contracts validate the result of the statement. The most typical use of this would be in validating the return value of a function and of any side effects it has. In D, pre contracts begin with in, and post contracts begin with out. They come at the end of the function signature and before the opening brace of the function body.
 +
 +<code D>
 +int fun(ref int a, int b)
 +in (a > 0)
 +in (b >= 0, "b cannot be negative!"​)
 +out (r; r > 0, "​return must be positive"​)
 +out (; a != 0)
 +{
 +    // function body
 +}
 +
 +int fun(ref int a, int b)
 +in
 +{
 +    assert(a > 0);
 +    assert(b >= 0, "b cannot be negative!"​);​
 +}
 +out (r)
 +{
 +    assert(r > 0, "​return must be positive"​);​
 +    assert(a != 0);
 +}
 +do
 +{
 +    // function body
 +}
 +</​code>​
 +
 +The two functions are almost identical semantically. The expressions in the first are lowered to contract blocks that look almost exactly like the second, except that a separate block is created for each expression in the first, thus avoiding shadowing variable names.
 +
 +=== Invariants ===
 +
 +Invariants are used to specify characteristics of a class or struct that must always be true (except while executing a member function). For example, a class representing a date might have an invariant that the day must be 1..31 and the hour must be 0..23:
 +
 +<code D>
 +class Date
 +{
 +    int day;
 +    int hour;
 +
 +    this(int d, int h)
 +    {
 +        day = d;
 +        hour = h;
 +    }
 +
 +    invariant
 +    {
 +        assert(1 <= day && day <= 31);
 +        assert(0 <= hour && hour < 24, "hour out of bounds"​);​
 +    }
 +}
 +</​code>​
 +
 +For public or exported functions, the order of execution is:
 +
 +  - preconditions
 +  - invariant
 +  - function body
 +  - invariant
 +  - postconditions
 +
 +===== 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 ====
 +
 +Go to this [[https://​github.com/​TheAlgorithms/​C/​tree/​master/​searching|link]]. You will find a series of searching algorithms, each implemented in C in its own file.
 +
 +  - Choose one algorithm and port it to D with minimum modifications.
 +  - Update the code using D specific features to improve the code (fewer lines of code, increase in expressiveness etc.).
 +
 +==== 2. Array Median Element ====
 +
 +Compute [[https://​www.geeksforgeeks.org/​median/​|the median]] element of an unsorted integer array. For this, you will have to:
 +
 +  - Sort the array: implement any sorting algorithm you wish.
 +  - Eliminate the duplicates: once the array is sorted, eliminating the duplicates is trivial.
 +  - Select the **(n+1)/​2th** element
 +
 +==== 3. The standard library ====
 +
 +D has a standard library called [[https://​dlang.org/​phobos/​|phobos]]. Implement exercise 2 using functions from the [[https://​dlang.org/​phobos/​std_algorithm.html|std.algorithm]] package. Use UFCS for an increase in expressiveness.
 +
 +==== 4. Majority Element ====
 +
 +Find the [[https://​leetcode.com/​problems/​majority-element/​|majority element]] in a string array using builtin associative arrays.
 +
 +**[Bonus]** Implement a version with **O(n)** time complexity and **O(1)** space complexity
 +
 +==== 5. Struct vs. Class ====
 +
 +Navigate to the ''​5-struct-class''​ directory. You will find 2 files implementing the same program. One uses a **struct** as a data container, while the other uses a **class**.
 +
 +  - 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?​
 +
 +==== 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.
dss/laboratoare/01.1559981395.txt.gz · Last modified: 2019/06/08 11:09 by razvan.nitu1305
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0