auto-sync

This commit is contained in:
rusEfi 2015-03-28 10:10:55 -05:00
parent f17a25641a
commit 34ffd33468
2 changed files with 49 additions and 16 deletions

View File

@ -36,6 +36,7 @@ class cyclic_buffer
void add(T value);
T get(int index);
T sum(int length);
T maxValue(int length);
void setSize(int size);
int getSize();
void clear();
@ -124,6 +125,27 @@ T cyclic_buffer<T>::get(int index) {
return elements[index];
}
template<typename T>
T cyclic_buffer<T>::maxValue(int length) {
if (length > count) {
length = count;
}
int ci = currentIndex; // local copy to increase thread-safety
T result = 0; // todo: better min value?
for (int i = 0; i < length; ++i) {
int index = ci - i;
while (index < 0) {
index += size;
}
if (elements[index] > result) {
result = elements[index];
}
}
return result;
}
template<typename T>
T cyclic_buffer<T>::sum(int length) {
if (length > count) {

View File

@ -55,6 +55,7 @@ void testOverflow64Counter(void) {
void testCyclicBuffer(void) {
print("*************************************** testCyclicBuffer\r\n");
{
sb.add(10);
assertEquals(10, sb.sum(3));
@ -62,6 +63,19 @@ void testCyclicBuffer(void) {
sb.add(2);
assertEquals(12, sb.sum(2));
}
{
sb.clear();
sb.add(1);
sb.add(2);
sb.add(3);
sb.add(4);
assertEquals(4, sb.maxValue(3));
assertEquals(4, sb.maxValue(113));
}
}
void testHistogram(void) {
print("******************************************* testHistogram\r\n");
@ -292,7 +306,6 @@ void testConsoleLogic(void) {
strcpy(buffer, "\"echo\"");
assertTrueM("unquote quoted", strEqual("echo", unquote(buffer)));
char *ptr = validateSecureLine(UNKNOWN_COMMAND);
assertEquals(0, strcmp(UNKNOWN_COMMAND, ptr));
assertEquals(10, tokenLength(UNKNOWN_COMMAND));
@ -321,12 +334,10 @@ void testConsoleLogic(void) {
assertEquals(111, atoi(lastFirst));
assertEquals(333, atoi(lastThird));
strcpy(buffer, "echosss \" 1\" 222 333");
handleConsoleLine(buffer);
assertTrue(strEqual("\" 1\"", lastFirst));
//addConsoleActionSSS("GPS", testGpsParser);
}