Skip to content

Commit

Permalink
clamp: Add.
Browse files Browse the repository at this point in the history
  • Loading branch information
daisukekoba committed Mar 29, 2020
1 parent 628440b commit 554e755
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
50 changes: 50 additions & 0 deletions clamp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*!
* \file
*/
/*
* zlib License
*
* Copyright (C) 2020 KOBAYASHI Daisuke
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef CLAMP_H_
#define CLAMP_H_

#ifndef CLAMP_T
#define CLAMP_T int
#endif

#define CLAMP_MIN(a, b) (((a) < (b)) ? (a) : (b))
#define CLAMP_MAX(a, b) (((a) > (b)) ? (a) : (b))

/** Clamps a variable to a given range.
*
* @param value value to clamp
* @param min lower limit
* @param max upper limit
*/
static inline CLAMP_T clamp(CLAMP_T value, CLAMP_T min, CLAMP_T max)
{
return CLAMP_MIN(CLAMP_MAX(value, min), max);
}

#undef CLAMP_MIN
#undef CLAMP_MAX

#endif /* CLAMP_H_ */
51 changes: 51 additions & 0 deletions test/clamp_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* zlib License
*
* Copyright (C) 2029 KOBAYASHI Daisuke
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#include <gtest/gtest.h>

extern "C" {
#include "clamp.h"
}

TEST(Clamp, Lower)
{
EXPECT_EQ(-10, clamp(-20, -10, 10));
EXPECT_EQ(0, clamp(-10, 0, 10));
EXPECT_EQ(200, clamp(100, 200, 300));
}

TEST(Clamp, Value)
{
EXPECT_EQ(-300, clamp(-300, -500, 100));
EXPECT_EQ(0, clamp(0, 0, 10));
EXPECT_EQ(1, clamp(1, 0, 10));
EXPECT_EQ(5, clamp(5, 0, 10));
EXPECT_EQ(10, clamp(10, 0, 10));
EXPECT_EQ(50, clamp(50, 0, 500));
EXPECT_EQ(1001, clamp(1001, 1000, 10000));
}
TEST(Clamp, Upper)
{
EXPECT_EQ(-20, clamp(10, -300, -20));
EXPECT_EQ(10, clamp(11, 0, 10));
EXPECT_EQ(1000, clamp(2000, 0, 1000));
}

0 comments on commit 554e755

Please sign in to comment.