Skip to content

Commit

Permalink
Fixed signed integer and float linearRand results #230
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Riccio committed Sep 6, 2014
1 parent 5f7862e commit bfa893b
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 18 deletions.
16 changes: 8 additions & 8 deletions glm/gtc/random.inl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace detail
{
GLM_FUNC_QUALIFIER static vecType<int8, P> call(vecType<int8, P> const & Min, vecType<int8, P> const & Max)
{
return (vecType<int8, P>(compute_rand<uint8, P, vecType>::call()) % (Max - Min)) + Min;
return (vecType<int8, P>(compute_rand<uint8, P, vecType>::call() % vecType<uint8, P>(Max + static_cast<int8>(1) - Min))) + Min;
}
};

Expand All @@ -140,7 +140,7 @@ namespace detail
{
GLM_FUNC_QUALIFIER static vecType<uint8, P> call(vecType<uint8, P> const & Min, vecType<uint8, P> const & Max)
{
return (compute_rand<uint8, P, vecType>::call() % (Max - Min)) + Min;
return (compute_rand<uint8, P, vecType>::call() % (Max + static_cast<uint8>(1) - Min)) + Min;
}
};

Expand All @@ -149,7 +149,7 @@ namespace detail
{
GLM_FUNC_QUALIFIER static vecType<int16, P> call(vecType<int16, P> const & Min, vecType<int16, P> const & Max)
{
return (vecType<int16, P>(compute_rand<uint16, P, vecType>::call()) % (Max - Min)) + Min;
return (vecType<int16, P>(compute_rand<uint16, P, vecType>::call() % vecType<uint16, P>(Max + static_cast<int16>(1) - Min))) + Min;
}
};

Expand All @@ -158,7 +158,7 @@ namespace detail
{
GLM_FUNC_QUALIFIER static vecType<uint16, P> call(vecType<uint16, P> const & Min, vecType<uint16, P> const & Max)
{
return (compute_rand<uint16, P, vecType>::call() % (Max - Min)) + Min;
return (compute_rand<uint16, P, vecType>::call() % (Max + static_cast<uint16>(1) - Min)) + Min;
}
};

Expand All @@ -167,7 +167,7 @@ namespace detail
{
GLM_FUNC_QUALIFIER static vecType<int32, P> call(vecType<int32, P> const & Min, vecType<int32, P> const & Max)
{
return (vecType<int32, P>(compute_rand<uint32, P, vecType>::call()) % (Max - Min)) + Min;
return (vecType<int32, P>(compute_rand<uint32, P, vecType>::call() % vecType<uint32, P>(Max + static_cast<int32>(1) - Min))) + Min;
}
};

Expand All @@ -176,7 +176,7 @@ namespace detail
{
GLM_FUNC_QUALIFIER static vecType<uint32, P> call(vecType<uint32, P> const & Min, vecType<uint32, P> const & Max)
{
return (compute_rand<uint32, P, vecType>::call() % (Max - Min)) + Min;
return (compute_rand<uint32, P, vecType>::call() % (Max + static_cast<uint32>(1) - Min)) + Min;
}
};

Expand All @@ -185,7 +185,7 @@ namespace detail
{
GLM_FUNC_QUALIFIER static vecType<int64, P> call(vecType<int64, P> const & Min, vecType<int64, P> const & Max)
{
return (vecType<int64, P>(compute_rand<uint64, P, vecType>::call()) % (Max - Min)) + Min;
return (vecType<int64, P>(compute_rand<uint64, P, vecType>::call() % vecType<uint64, P>(Max + static_cast<int64>(1) - Min))) + Min;
}
};

Expand All @@ -194,7 +194,7 @@ namespace detail
{
GLM_FUNC_QUALIFIER static vecType<uint64, P> call(vecType<uint64, P> const & Min, vecType<uint64, P> const & Max)
{
return (compute_rand<uint64, P, vecType>::call() % (Max - Min)) + Min;
return (compute_rand<uint64, P, vecType>::call() % (Max + static_cast<uint64>(1) - Min)) + Min;
}
};

Expand Down
192 changes: 182 additions & 10 deletions test/gtc/gtc_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,195 @@ int test_linearRand()
{
int Error = 0;

glm::int32 const Min = 16;
glm::int32 const Max = 32;

{
glm::i8vec2 A = glm::linearRand(glm::i8vec2(16), glm::i8vec2(32));
glm::i16vec2 B = glm::linearRand(glm::i16vec2(16), glm::i16vec2(32));
glm::i32vec2 C = glm::linearRand(glm::i32vec2(16), glm::i32vec2(32));
glm::i64vec2 D = glm::linearRand(glm::i64vec2(16), glm::i64vec2(32));
glm::u8vec2 AMin(std::numeric_limits<glm::u8>::max());
glm::u8vec2 AMax(std::numeric_limits<glm::u8>::min());
{
for(std::size_t i = 0; i < 100000; ++i)
{
glm::u8vec2 A = glm::linearRand(glm::u8vec2(Min), glm::u8vec2(Max));
AMin = glm::min(AMin, A);
AMax = glm::max(AMax, A);

if(!glm::all(glm::lessThanEqual(A, glm::u8vec2(Max))))
++Error;
if(!glm::all(glm::greaterThanEqual(A, glm::u8vec2(Min))))
++Error;
assert(!Error);
}

Error += glm::all(glm::equal(AMin, glm::u8vec2(Min))) ? 0 : 1;
Error += glm::all(glm::equal(AMax, glm::u8vec2(Max))) ? 0 : 1;
assert(!Error);
}

glm::u16vec2 BMin(std::numeric_limits<glm::u16>::max());
glm::u16vec2 BMax(std::numeric_limits<glm::u16>::min());
{
for(std::size_t i = 0; i < 100000; ++i)
{
glm::u16vec2 B = glm::linearRand(glm::u16vec2(Min), glm::u16vec2(Max));
BMin = glm::min(BMin, B);
BMax = glm::max(BMax, B);

if(!glm::all(glm::lessThanEqual(B, glm::u16vec2(Max))))
++Error;
if(!glm::all(glm::greaterThanEqual(B, glm::u16vec2(Min))))
++Error;
assert(!Error);
}

Error += glm::all(glm::equal(BMin, glm::u16vec2(Min))) ? 0 : 1;
Error += glm::all(glm::equal(BMax, glm::u16vec2(Max))) ? 0 : 1;
assert(!Error);
}

glm::u32vec2 CMin(std::numeric_limits<glm::u32>::max());
glm::u32vec2 CMax(std::numeric_limits<glm::u32>::min());
{
for(std::size_t i = 0; i < 100000; ++i)
{
glm::u32vec2 C = glm::linearRand(glm::u32vec2(Min), glm::u32vec2(Max));
CMin = glm::min(CMin, C);
CMax = glm::max(CMax, C);

if(!glm::all(glm::lessThanEqual(C, glm::u32vec2(Max))))
++Error;
if(!glm::all(glm::greaterThanEqual(C, glm::u32vec2(Min))))
++Error;
assert(!Error);
}

Error += glm::all(glm::equal(CMin, glm::u32vec2(Min))) ? 0 : 1;
Error += glm::all(glm::equal(CMax, glm::u32vec2(Max))) ? 0 : 1;
assert(!Error);
}

glm::u64vec2 DMin(std::numeric_limits<glm::u64>::max());
glm::u64vec2 DMax(std::numeric_limits<glm::u64>::min());
{
for(std::size_t i = 0; i < 100000; ++i)
{
glm::u64vec2 D = glm::linearRand(glm::u64vec2(Min), glm::u64vec2(Max));
DMin = glm::min(DMin, D);
DMax = glm::max(DMax, D);

if(!glm::all(glm::lessThanEqual(D, glm::u64vec2(Max))))
++Error;
if(!glm::all(glm::greaterThanEqual(D, glm::u64vec2(Min))))
++Error;
assert(!Error);
}

Error += glm::all(glm::equal(DMin, glm::u64vec2(Min))) ? 0 : 1;
Error += glm::all(glm::equal(DMax, glm::u64vec2(Max))) ? 0 : 1;
assert(!Error);
}
}

{
glm::u8vec2 A = glm::linearRand(glm::u8vec2(16), glm::u8vec2(32));
glm::u16vec2 B = glm::linearRand(glm::u16vec2(16), glm::u16vec2(32));
glm::u32vec2 C = glm::linearRand(glm::u32vec2(16), glm::u32vec2(32));
glm::u64vec2 D = glm::linearRand(glm::u64vec2(16), glm::u64vec2(32));
glm::i8vec2 AMin(std::numeric_limits<glm::i8>::max());
glm::i8vec2 AMax(std::numeric_limits<glm::i8>::min());
{
for(std::size_t i = 0; i < 100000; ++i)
{
glm::i8vec2 A = glm::linearRand(glm::i8vec2(Min), glm::i8vec2(Max));
AMin = glm::min(AMin, A);
AMax = glm::max(AMax, A);

if(!glm::all(glm::lessThanEqual(A, glm::i8vec2(Max))))
++Error;
if(!glm::all(glm::greaterThanEqual(A, glm::i8vec2(Min))))
++Error;
assert(!Error);
}

Error += glm::all(glm::equal(AMin, glm::i8vec2(Min))) ? 0 : 1;
Error += glm::all(glm::equal(AMax, glm::i8vec2(Max))) ? 0 : 1;
assert(!Error);
}

glm::i16vec2 BMin(std::numeric_limits<glm::i16>::max());
glm::i16vec2 BMax(std::numeric_limits<glm::i16>::min());
{
for(std::size_t i = 0; i < 100000; ++i)
{
glm::i16vec2 B = glm::linearRand(glm::i16vec2(Min), glm::i16vec2(Max));
BMin = glm::min(BMin, B);
BMax = glm::max(BMax, B);

if(!glm::all(glm::lessThanEqual(B, glm::i16vec2(Max))))
++Error;
if(!glm::all(glm::greaterThanEqual(B, glm::i16vec2(Min))))
++Error;
assert(!Error);
}

Error += glm::all(glm::equal(BMin, glm::i16vec2(Min))) ? 0 : 1;
Error += glm::all(glm::equal(BMax, glm::i16vec2(Max))) ? 0 : 1;
assert(!Error);
}

glm::i32vec2 CMin(std::numeric_limits<glm::i32>::max());
glm::i32vec2 CMax(std::numeric_limits<glm::i32>::min());
{
for(std::size_t i = 0; i < 100000; ++i)
{
glm::i32vec2 C = glm::linearRand(glm::i32vec2(Min), glm::i32vec2(Max));
CMin = glm::min(CMin, C);
CMax = glm::max(CMax, C);

if(!glm::all(glm::lessThanEqual(C, glm::i32vec2(Max))))
++Error;
if(!glm::all(glm::greaterThanEqual(C, glm::i32vec2(Min))))
++Error;
assert(!Error);
}

Error += glm::all(glm::equal(CMin, glm::i32vec2(Min))) ? 0 : 1;
Error += glm::all(glm::equal(CMax, glm::i32vec2(Max))) ? 0 : 1;
assert(!Error);
}

glm::i64vec2 DMin(std::numeric_limits<glm::i64>::max());
glm::i64vec2 DMax(std::numeric_limits<glm::i64>::min());
{
for(std::size_t i = 0; i < 100000; ++i)
{
glm::i64vec2 D = glm::linearRand(glm::i64vec2(Min), glm::i64vec2(Max));
DMin = glm::min(DMin, D);
DMax = glm::max(DMax, D);

if(!glm::all(glm::lessThanEqual(D, glm::i64vec2(Max))))
++Error;
if(!glm::all(glm::greaterThanEqual(D, glm::i64vec2(Min))))
++Error;
assert(!Error);
}

Error += glm::all(glm::equal(DMin, glm::i64vec2(Min))) ? 0 : 1;
Error += glm::all(glm::equal(DMax, glm::i64vec2(Max))) ? 0 : 1;
assert(!Error);
}
}

for(std::size_t i = 0; i < 100000; ++i)
{
glm::f32vec2 A = glm::linearRand(glm::f32vec2(16), glm::f32vec2(32));
glm::f64vec2 B = glm::linearRand(glm::f64vec2(16), glm::f64vec2(32));
glm::f32vec2 const A(glm::linearRand(glm::f32vec2(Min), glm::f32vec2(Max)));
if(!glm::all(glm::lessThanEqual(A, glm::f32vec2(Max))))
++Error;
if(!glm::all(glm::greaterThanEqual(A, glm::f32vec2(Min))))
++Error;

glm::f64vec2 const B(glm::linearRand(glm::f64vec2(Min), glm::f64vec2(Max)));
if(!glm::all(glm::lessThanEqual(B, glm::f64vec2(Max))))
++Error;
if(!glm::all(glm::greaterThanEqual(B, glm::f64vec2(Min))))
++Error;
assert(!Error);
}

{
Expand Down

0 comments on commit bfa893b

Please sign in to comment.