diff --git a/1_longest_substring_with_k_distinct_characters.cpp b/1_longest_substring_with_k_distinct_characters.cpp index c5bb10c..28c733b 100644 --- a/1_longest_substring_with_k_distinct_characters.cpp +++ b/1_longest_substring_with_k_distinct_characters.cpp @@ -49,12 +49,12 @@ public: unsigned long longest_start = 0; unsigned long longest_end = 0; bool match_found = false; - bool operator==(const result r); + bool operator==(const result r) const; friend std::istream &operator>>(std::istream &in, result &r); friend std::ostream &operator<<(std::ostream &out, const result &r); }; -bool result::operator==(const result r) { +bool result::operator==(const result r) const { return longest_start == r.longest_start && longest_end == r.longest_end && match_found == r.match_found; } @@ -151,6 +151,20 @@ bool K_verify(const test_case &t) { return unique_chars.size() == t.i.k; } +bool thorough_test(const test_case &t) { + unsigned long org_window_size = t.r.longest_end - t.r.longest_start + 1; + if (org_window_size >= t.i.s.length()) + return true; + for (unsigned long i = 0, n_tests = t.i.s.length() - org_window_size; + i < n_tests; i++) { + result r = {i, i + org_window_size, true}; + test_case mock_test = {t.i, r}; + if (K_verify(mock_test)) + return false; + } + return true; +} + int main(int argc, char *argv[]) { std::cout << "Processing static test cases from input file (if any)\n"; semicolon_is_space delimeter; @@ -164,41 +178,47 @@ int main(int argc, char *argv[]) { test(t); } } + } - std::cout - << "\n\n\nPerforming metamorphic tests from randomly generated input " - "(string and K values)" - << std::endl; - random_number_generator random_ascii_char(97, 122); // a-z - random_number_generator rng; - for (unsigned int i = 0; i < 10; i++) { - test_case t; - t.i.s += (char)random_ascii_char(); - t.r = find(t.i.s, t.i.k); - std::cout << t; - for (unsigned int j = 0; j < 10; j++) { - for (unsigned int k = 0, increase_str_len_by = rng(100, 1000); - k < increase_str_len_by; k++) { - t.i.s += (char)random_ascii_char(); - result r = find(t.i.s, t.i.k); - if (r.match_found && t.r.match_found) - assert(r.longest_end - r.longest_start >= - t.r.longest_end - t.r.longest_start); - t.r = r; - std::cout << t; - assert(K_verify(t)); - } - while (t.r.match_found) { - result r = find(t.i.s, ++t.i.k); - if (r.match_found) - assert(r.longest_end - r.longest_start >= - t.r.longest_end - t.r.longest_start); - t.r = r; - std::cout << t; - assert(K_verify(t)); - } - t.i.k = rng(1, --t.i.k); + // Thorough test (100% verifiable correct) is performaned randomly in ~1% + // cases + std::cout << "\n\n\nPerforming metamorphic tests from randomly generated " + "input (string and K values).\nThorough test (100\% verifiable " + "correct) is performaned randomly in ~1\% cases." + << std::endl; + random_number_generator random_ascii_char(97, 122); // a-z + random_number_generator rng; + for (unsigned int i = 0; i < 10; i++) { + test_case t; + t.i.s += (char)random_ascii_char(); + t.r = find(t.i.s, t.i.k); + std::cout << t; + for (unsigned int j = 0; j < 10; j++) { + for (unsigned int k = 0, increase_str_len_by = rng(100, 1000); + k < increase_str_len_by; k++) { + t.i.s += (char)random_ascii_char(); + result r = find(t.i.s, t.i.k); + if (r.match_found && t.r.match_found) + assert(r.longest_end - r.longest_start >= + t.r.longest_end - t.r.longest_start); + t.r = r; + std::cout << t; + assert(K_verify(t)); + if (!rng(0, 99)) + assert(thorough_test(t)); + } + while (t.r.match_found) { + result r = find(t.i.s, ++t.i.k); + if (r.match_found) + assert(r.longest_end - r.longest_start >= + t.r.longest_end - t.r.longest_start); + t.r = r; + std::cout << t; + assert(K_verify(t)); + if (!rng(0, 99)) + assert(thorough_test(t)); } + t.i.k = rng(1, --t.i.k); } } return 0;