|
| 1 | +/******************************************************* |
| 2 | + * Copyright (c) 2014, ArrayFire |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This file is distributed under 3-clause BSD license. |
| 6 | + * The complete license agreement can be obtained at: |
| 7 | + * http://arrayfire.com/licenses/BSD-3-Clause |
| 8 | + ********************************************************/ |
| 9 | + |
| 10 | +#include <gtest/gtest.h> |
| 11 | +#include <af/array.h> |
| 12 | +#include <af/arith.h> |
| 13 | +#include <af/data.h> |
| 14 | +#include <testHelpers.hpp> |
| 15 | + |
| 16 | +using namespace std; |
| 17 | +using std::abs; |
| 18 | +using namespace af; |
| 19 | + |
| 20 | +const int num = 10000; |
| 21 | + |
| 22 | +TEST(ClampTests, FloatArrayArray) |
| 23 | +{ |
| 24 | + array in = af::randu(num, f32); |
| 25 | + array lo = af::randu(num, f32)/10; // Ensure lo <= 0.1 |
| 26 | + array hi = 1.0 - af::randu(num, f32)/10; // Ensure hi >= 0.9 |
| 27 | + af::eval(lo, hi); |
| 28 | + |
| 29 | + |
| 30 | + std::vector<float> hout(num), hin(num), hlo(num), hhi(num); |
| 31 | + array out = clamp(in, lo, hi); |
| 32 | + out.host(&hout[0]); |
| 33 | + in.host(&hin[0]); |
| 34 | + lo.host(&hlo[0]); |
| 35 | + hi.host(&hhi[0]); |
| 36 | + |
| 37 | + for (int i = 0; i < num; i++) { |
| 38 | + ASSERT_LE(hout[i], hhi[i]); |
| 39 | + ASSERT_GE(hout[i], hlo[i]); |
| 40 | + ASSERT_EQ(true, hout[i] == hin[i] || hout[i] == hlo[i] || hout[i] == hhi[i]); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +TEST(ClampTests, FloatArrayScalar) |
| 45 | +{ |
| 46 | + array in = af::randu(num, f32); |
| 47 | + array lo = af::randu(num, f32)/10; // Ensure lo <= 0.1 |
| 48 | + float hi = 0.9; |
| 49 | + |
| 50 | + std::vector<float> hout(num), hin(num), hlo(num); |
| 51 | + array out = clamp(in, lo, hi); |
| 52 | + |
| 53 | + out.host(&hout[0]); |
| 54 | + in.host(&hin[0]); |
| 55 | + lo.host(&hlo[0]); |
| 56 | + |
| 57 | + for (int i = 0; i < num; i++) { |
| 58 | + ASSERT_LE(hout[i], hi); |
| 59 | + ASSERT_GE(hout[i], hlo[i]); |
| 60 | + ASSERT_EQ(true, hout[i] == hin[i] || hout[i] == hlo[i] || hout[i] == hi); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +TEST(ClampTests, FloatScalarArray) |
| 65 | +{ |
| 66 | + array in = af::randu(num, f32); |
| 67 | + float lo = 0.1; |
| 68 | + array hi = 1.0 - af::randu(num, f32)/10; // Ensure hi >= 0.9 |
| 69 | + |
| 70 | + std::vector<float> hout(num), hin(num), hhi(num); |
| 71 | + array out = clamp(in, lo, hi); |
| 72 | + |
| 73 | + out.host(&hout[0]); |
| 74 | + in.host(&hin[0]); |
| 75 | + hi.host(&hhi[0]); |
| 76 | + |
| 77 | + for (int i = 0; i < num; i++) { |
| 78 | + ASSERT_LE(hout[i], hhi[i]); |
| 79 | + ASSERT_GE(hout[i], lo); |
| 80 | + ASSERT_EQ(true, hout[i] == hin[i] || hout[i] == lo || hout[i] == hhi[i]); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +TEST(ClampTests, FloatScalarScalar) |
| 85 | +{ |
| 86 | + array in = af::randu(num, f32); |
| 87 | + float lo = 0.1; |
| 88 | + float hi = 0.9; |
| 89 | + |
| 90 | + std::vector<float> hout(num), hin(num); |
| 91 | + array out = clamp(in, lo, hi); |
| 92 | + |
| 93 | + out.host(&hout[0]); |
| 94 | + in.host(&hin[0]); |
| 95 | + |
| 96 | + for (int i = 0; i < num; i++) { |
| 97 | + ASSERT_LE(hout[i], hi); |
| 98 | + ASSERT_GE(hout[i], lo); |
| 99 | + ASSERT_EQ(true, hout[i] == hin[i] || hout[i] == lo || hout[i] == hi); |
| 100 | + } |
| 101 | +} |
0 commit comments