mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 #include "test_env.h"
elessair 0:f269e3021894 2
elessair 0:f269e3021894 3 #include <algorithm>
elessair 0:f269e3021894 4 #include <string>
elessair 0:f269e3021894 5 #include <vector>
elessair 0:f269e3021894 6 #include <queue>
elessair 0:f269e3021894 7 #include <map>
elessair 0:f269e3021894 8 #include <math.h>
elessair 0:f269e3021894 9
elessair 0:f269e3021894 10 #define BUFFER_SIZE 128
elessair 0:f269e3021894 11 #define TABLE_SIZE(TAB) (sizeof(TAB) / sizeof(TAB[0]))
elessair 0:f269e3021894 12
elessair 0:f269e3021894 13 #define NEGATIVE_INTEGERS -32768,-3214,-999,-100,-1,0,1,4231,999,4123,32760,99999
elessair 0:f269e3021894 14 #define POSITIVE_INTEGERS 32768,3214,999,100,1,0,1,4231,999,4123,32760,99999
elessair 0:f269e3021894 15 #define FLOATS 0.002,0.92430,15.91320,791.77368,6208.2,25719.4952,426815.982588,6429271.046,42468024.93,212006462.910
elessair 0:f269e3021894 16 #define FLOATS_STR "0.002","0.92430","15.91320","791.77368","6208.2","25719.4952","426815.982588","6429271.046","42468024.93","212006462.910"
elessair 0:f269e3021894 17
elessair 0:f269e3021894 18 template <class T, class F>
elessair 0:f269e3021894 19 void BubbleSort(T& array, size_t array_size, F functor)
elessair 0:f269e3021894 20 {
elessair 0:f269e3021894 21 bool flag = true;
elessair 0:f269e3021894 22 size_t numLength = array_size;
elessair 0:f269e3021894 23 for(size_t i = 1; (i <= numLength) && flag; i++) {
elessair 0:f269e3021894 24 flag = false;
elessair 0:f269e3021894 25 for (size_t j = 0; j < (numLength - 1); j++) {
elessair 0:f269e3021894 26 if (functor(array[j+1], array[j])) {
elessair 0:f269e3021894 27 int temp = array[j];
elessair 0:f269e3021894 28 array[j] = array[j + 1];
elessair 0:f269e3021894 29 array[j+1] = temp;
elessair 0:f269e3021894 30 flag = true;
elessair 0:f269e3021894 31 }
elessair 0:f269e3021894 32 }
elessair 0:f269e3021894 33 }
elessair 0:f269e3021894 34 }
elessair 0:f269e3021894 35
elessair 0:f269e3021894 36 struct printInt {
elessair 0:f269e3021894 37 void operator()(int i) {
elessair 0:f269e3021894 38 printf("%d ", i);
elessair 0:f269e3021894 39 }
elessair 0:f269e3021894 40 };
elessair 0:f269e3021894 41
elessair 0:f269e3021894 42 struct printFloat {
elessair 0:f269e3021894 43 void operator()(float f) {
elessair 0:f269e3021894 44 printf("%f ", f);
elessair 0:f269e3021894 45 }
elessair 0:f269e3021894 46 };
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 struct printString {
elessair 0:f269e3021894 49 void operator()(char* s) {
elessair 0:f269e3021894 50 printf("%s ", s);
elessair 0:f269e3021894 51 }
elessair 0:f269e3021894 52 };
elessair 0:f269e3021894 53
elessair 0:f269e3021894 54 struct greaterAbs {
elessair 0:f269e3021894 55 bool operator()(int a, int b) {
elessair 0:f269e3021894 56 return abs(a) > abs(b);
elessair 0:f269e3021894 57 }
elessair 0:f269e3021894 58 };
elessair 0:f269e3021894 59
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 int main()
elessair 0:f269e3021894 62 {
elessair 0:f269e3021894 63 int p_integers[] = {POSITIVE_INTEGERS};
elessair 0:f269e3021894 64 int n_integers[] = {NEGATIVE_INTEGERS};
elessair 0:f269e3021894 65 float floats[] = {FLOATS};
elessair 0:f269e3021894 66 bool result = true;
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 {
elessair 0:f269e3021894 69 std::vector<int> v_pints(p_integers, p_integers + TABLE_SIZE(p_integers));
elessair 0:f269e3021894 70 bool equal_result = std::equal(v_pints.begin(), v_pints.end(), p_integers);
elessair 0:f269e3021894 71 result = result && equal_result;
elessair 0:f269e3021894 72 printf("[%s] Fill vector with data\r\n", equal_result ? "OK" : "FAIL");
elessair 0:f269e3021894 73 }
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75 {
elessair 0:f269e3021894 76 char* floats_str[] = {FLOATS_STR};
elessair 0:f269e3021894 77 float floats_transform[TABLE_SIZE(floats_str)] = {0.0};
elessair 0:f269e3021894 78 std::transform(floats_str, floats_str + TABLE_SIZE(floats_str), floats_transform, atof);
elessair 0:f269e3021894 79 bool equal_result = std::equal(floats_transform, floats_transform + TABLE_SIZE(floats_transform), floats);
elessair 0:f269e3021894 80 result = result && equal_result;
elessair 0:f269e3021894 81 printf("[%s] Transform float strings\r\n", equal_result ? "OK" : "FAIL");
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83 std::for_each(floats_str, floats_str + TABLE_SIZE(floats_str), printString());
elessair 0:f269e3021894 84 printf("\r\n");
elessair 0:f269e3021894 85 std::for_each(floats_transform, floats_transform + TABLE_SIZE(floats_transform), printFloat());
elessair 0:f269e3021894 86 printf("\r\n");
elessair 0:f269e3021894 87 }
elessair 0:f269e3021894 88
elessair 0:f269e3021894 89 {
elessair 0:f269e3021894 90 std::vector<int> v_nints_1(n_integers, n_integers + TABLE_SIZE(n_integers));
elessair 0:f269e3021894 91 std::vector<int> v_nints_2(n_integers, n_integers + TABLE_SIZE(n_integers));
elessair 0:f269e3021894 92 {
elessair 0:f269e3021894 93 BubbleSort(v_nints_1, v_nints_1.size(), std::greater<int>());
elessair 0:f269e3021894 94 std::sort(v_nints_2.begin(), v_nints_2.end(), std::greater<int>());
elessair 0:f269e3021894 95 bool equal_result = std::equal(v_nints_1.begin(), v_nints_1.end(), v_nints_2.begin());
elessair 0:f269e3021894 96 result = result && equal_result;
elessair 0:f269e3021894 97 printf("[%s] Sort integers\r\n", equal_result ? "OK" : "FAIL");
elessair 0:f269e3021894 98
elessair 0:f269e3021894 99 std::for_each(v_nints_1.begin(), v_nints_1.end(), printInt());
elessair 0:f269e3021894 100 printf("\r\n");
elessair 0:f269e3021894 101 std::for_each(v_nints_2.begin(), v_nints_2.end(), printInt());
elessair 0:f269e3021894 102 printf("\r\n");
elessair 0:f269e3021894 103 }
elessair 0:f269e3021894 104
elessair 0:f269e3021894 105 {
elessair 0:f269e3021894 106 BubbleSort(v_nints_1, v_nints_1.size(), greaterAbs());
elessair 0:f269e3021894 107 std::sort(v_nints_2.begin(), v_nints_2.end(), greaterAbs());
elessair 0:f269e3021894 108 bool equal_result = std::equal(v_nints_1.begin(), v_nints_1.end(), v_nints_2.begin());
elessair 0:f269e3021894 109 result = result && equal_result;
elessair 0:f269e3021894 110 printf("[%s] Sort integers\r\n", equal_result ? "OK" : "FAIL");
elessair 0:f269e3021894 111
elessair 0:f269e3021894 112 std::for_each(v_nints_1.begin(), v_nints_1.end(), printInt());
elessair 0:f269e3021894 113 printf("\r\n");
elessair 0:f269e3021894 114 std::for_each(v_nints_2.begin(), v_nints_2.end(), printInt());
elessair 0:f269e3021894 115 printf("\r\n");
elessair 0:f269e3021894 116 }
elessair 0:f269e3021894 117 }
elessair 0:f269e3021894 118
elessair 0:f269e3021894 119 notify_completion(result);
elessair 0:f269e3021894 120 return 0;
elessair 0:f269e3021894 121 }