add tk_round/tk_clamp

This commit is contained in:
xianjimli 2018-10-31 13:49:19 +08:00
parent 6c27fe3655
commit e2ab3abc1b
3 changed files with 24 additions and 12 deletions

View File

@ -1,4 +1,4 @@
/**
/**
* File: types_def.h
* Author: AWTK Develop Team
* Brief: basic types definitions.
@ -245,17 +245,11 @@ typedef enum _lcd_orientation_t {
typedef void* pointer_t;
#ifndef tk_min
#define tk_min(a, b) ((a) < (b) ? (a) : (b))
#endif /*tk_min*/
#ifndef tk_abs
#define tk_abs(a) ((a) < (0) ? (-a) : (a))
#endif /*tk_abs*/
#ifndef tk_max
#define tk_max(a, b) ((a) > (b) ? (a) : (b))
#endif /*tk_max*/
#define tk_roundi(a) (int32_t)(((a) >= 0) ? ((a) + 0.5f) : ((a)-0.5f))
#define tk_clampi(a, mn, mx) ((a) < (mn) ? (mn) : ((a) > (mx) ? (mx) : (a)))
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

4
src/ext_widgets/text_selector/text_selector.c Normal file → Executable file
View File

@ -1,4 +1,4 @@
/**
/**
* File: text_selector.h
* Author: AWTK Develop Team
* Brief: text_selector
@ -337,7 +337,7 @@ static ret_t text_selector_on_pointer_up(text_selector_t* text_selector, pointer
yoffset_end = max_yoffset;
}
yoffset_end = round((float)yoffset_end / (float)item_height) * item_height;
yoffset_end = tk_roundi((float)yoffset_end / (float)item_height) * item_height;
text_selector_scroll_to(widget, yoffset_end);
return RET_OK;

View File

@ -1,7 +1,7 @@
#include "base/types_def.h"
#include "gtest/gtest.h"
TEST(Basic, round) {
TEST(Basic, round_to) {
uint32_t n = 4;
uint32_t i = 0;
for (i = 1; i < n; i++) {
@ -23,3 +23,21 @@ TEST(Basic, round) {
ASSERT_EQ(TK_ROUND_TO(i, n), n);
}
}
TEST(Basic, round) {
ASSERT_EQ(tk_roundi(1.1), 1);
ASSERT_EQ(tk_roundi(1.5), 2);
ASSERT_EQ(tk_roundi(1.9), 2);
ASSERT_EQ(tk_roundi(-1.1), -1);
ASSERT_EQ(tk_roundi(-1.5), -2);
ASSERT_EQ(tk_roundi(-1.9), -2);
}
TEST(Basic, clamp) {
ASSERT_EQ(tk_clampi(1, 5, 8), 5);
ASSERT_EQ(tk_clampi(10, 5, 8), 8);
ASSERT_EQ(tk_clampi(5, 5, 8), 5);
ASSERT_EQ(tk_clampi(8, 5, 8), 8);
ASSERT_EQ(tk_clampi(6, 5, 8), 6);
}