130 likes | 221 Views
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 ;
E N D
Pre/Post Condition Discussion 03/13/2013
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); }
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
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]; }
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]
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; }
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]
Quicksort void call_sort(struct Sorter* sorter_) { if (sorter_->sort_fn != NULL) { sorter_->sort_fn(sorter_->numbers_, 0, SIZE-1); } }
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]
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 }
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]
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]
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?