1 / 13

Pre/Post Condition Discussion

Pre/Post Condition Discussion. 03 / 13 / 2013. Quicksort. int main( int argc , char* argv []) { int numbers[SIZE] = {2,5,3…} struct Sorter s ; // initialize generic data producer setup_data (&s, ( int *)numbers); int * (* qs )( int *, int , int ) = &quicksort; s.sort_fn = qs ;

melia
Download Presentation

Pre/Post Condition Discussion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pre/Post Condition Discussion 03/13/2013

  2. Quicksort int main(intargc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic data producer setup_data(&s, (int*)numbers); int* (*qs)(int*, int, int) = &quicksort; s.sort_fn = qs; call_sort(&s); }

  3. Quicksort int main(intargc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic data producer setup_data(&s, (int*)numbers); int* (*qs)(int*, int, int) = &quicksort; s.sort_fn = qs; call_sort(&s); } Functions we care about providing pre/postconditions for as they are a part of the composition strategy

  4. Quicksort numbers[SIZE] = {2,3,5…} // Generic sorting class void setup_data(struct Sorter* sorter_, int* input) { inti; // store initial dataset internally for (i = 0; i < SIZE; ++i) sorter_->numbers_[i] = input[i]; }

  5. Quicksort numbers[SIZE] = {2,3,5…} // Generic sorting class void setup_data(struct Sorter* sorter_, int* input) { inti; // store initial dataset internally for (i = 0; i < SIZE; ++i) sorter_->numbers_[i] = input[i]; } // precondition: sorter_ != NULL // precondition: input != NULL // postcondition: for all i, sorter_->numbers[i] == numbers[i]

  6. Quicksort // quicksort recursive function int* quicksort(int* input, int p, int r) { int *ptr; ptr = input; if (p < r) { int j = partition(input, p, r); quicksort(input, p, j-1); quicksort(input, j+1, r); } return ptr; }

  7. Quicksort // quicksort recursive function int* quicksort(int* input, int p, int r) { int *ptr; ptr = input; if (p < r) { int j = partition(input, p, r); quicksort(input, p, j-1); quicksort(input, j+1, r); } return ptr; } // precondition: input != NULL // precondition: p != r // postcondition: input[0] <= ... <= input[SIZE-1]

  8. Quicksort void call_sort(struct Sorter* sorter_) { if (sorter_->sort_fn != NULL) { sorter_->sort_fn(sorter_->numbers_, 0, SIZE-1); } }

  9. Quicksort void call_sort(struct Sorter* sorter_) { if (sorter_->sort_fn != NULL) { sorter_->sort_fn(sorter_->numbers_, 0, SIZE-1); } } // precondition: sorter_ != NULL // precondition: sorter_->sort_fn != NULL // postcondition: sorter_->numbers_ <= ... <= sorter_->numbers_[SIZE-1]

  10. Mergesort (with strings) void mergesort_wrapper(char* input[]); intmain(intargc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array }

  11. Mergesort (with strings) void mergesort_wrapper(char* input[]); intmain(intargc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array } // precondition-desc input must be converted to/from integer ??? // precondition-desc: input = array of strings // precondition: input != NULL // postcondition-desc: input = sorted array of strings // postcondition: input[0] <= ... <= input[SIZE-1]

  12. Mergesort (with strings) void mergesort_wrapper(char* input[]); intmain(intargc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array } Necessary information for CFG? // precondition-desc input must be converted to/from integer ??? // precondition-desc: input = array of strings // precondition: input != NULL // postcondition-desc: input = sorted array of strings // postcondition: input[0] <= ... <= input[SIZE-1]

  13. Mergesort (with strings) void mergesort_wrapper(char* input[]); intmain(intargc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array } • How do we formally specify where to insert composed code? • How do we formally specify necessary behavioral information? • e.g. input must be converted to int array before being passed to merge sort • Precondition? • Method signature?

More Related