Thread overview
Idiomatic way to generate all possible values a static array of ubyte can have
Apr 02, 2016
jkpl
Apr 02, 2016
rikki cattermole
Apr 02, 2016
jkpl
Apr 02, 2016
rikki cattermole
Apr 02, 2016
jkpl
Apr 02, 2016
jkpl
Apr 02, 2016
jkpl
Apr 02, 2016
jkpl
Apr 02, 2016
Ali Çehreli
April 02, 2016
Let's say I have a ubyte[256]. I want to test all the possible values this array can have on a function.

Currently I fill it for each new test with std.random.uniform but I'm sure that I loose some time with randomizing and with the tests that are repeated. Is there a simple way to do this ? Without thinking much, it seems that the loops to generate this would be seriously awefull.

By thinking a bit more maybe with a fixed-size big integer it would work, since the value could be casted as the right array type (as long as its length is power of 2) ?
April 02, 2016
On 02/04/2016 9:20 PM, jkpl wrote:
> Let's say I have a ubyte[256]. I want to test all the possible values
> this array can have on a function.
>
> Currently I fill it for each new test with std.random.uniform but I'm
> sure that I loose some time with randomizing and with the tests that are
> repeated. Is there a simple way to do this ? Without thinking much, it
> seems that the loops to generate this would be seriously awefull.
>
> By thinking a bit more maybe with a fixed-size big integer it would
> work, since the value could be casted as the right array type (as long
> as its length is power of 2) ?

Actually I'd use a hex string.

So:
static ubyte[256] DATA = cast(ubyte[256])x"00 01 02 03";
I'm sure you get the idea. That can be created easily enough.
April 02, 2016
On Saturday, 2 April 2016 at 08:27:07 UTC, rikki cattermole wrote:
> On 02/04/2016 9:20 PM, jkpl wrote:
>> Let's say I have a ubyte[256]. I want to test all the possible values
>> this array can have on a function.
>
> Actually I'd use a hex string.
>
> So:
> static ubyte[256] DATA = cast(ubyte[256])x"00 01 02 03";
> I'm sure you get the idea. That can be created easily enough.

No I don't get the idea. I think that I haven't well explained. By static array I meant "not dynamic". The 256^256 combinations must be generated on the same ubyte[256], which is a variable, during the program execution.
April 02, 2016
On 02/04/2016 9:36 PM, jkpl wrote:
> On Saturday, 2 April 2016 at 08:27:07 UTC, rikki cattermole wrote:
>> On 02/04/2016 9:20 PM, jkpl wrote:
>>> Let's say I have a ubyte[256]. I want to test all the possible values
>>> this array can have on a function.
>>
>> Actually I'd use a hex string.
>>
>> So:
>> static ubyte[256] DATA = cast(ubyte[256])x"00 01 02 03";
>> I'm sure you get the idea. That can be created easily enough.
>
> No I don't get the idea. I think that I haven't well explained. By
> static array I meant "not dynamic". The 256^256 combinations must be
> generated on the same ubyte[256], which is a variable, during the
> program execution.

Okay that is a problem then.
Stick with generating it I think, can't hard code that number of combinations.
April 02, 2016
On Saturday, 2 April 2016 at 08:48:10 UTC, rikki cattermole wrote:
> On 02/04/2016 9:36 PM, jkpl wrote:
>> On Saturday, 2 April 2016 at 08:27:07 UTC, rikki cattermole wrote:
> Okay that is a problem then.

Yes clearly!

Maybe this, a bit better:


    foreach (b0; randomCover(iota(0,256)))
    foreach (b1; randomCover(iota(0,256)))
    ...
    foreach (b255; randomCover(iota(0,256)))
    {
        ...
    }
April 02, 2016
On Saturday, 2 April 2016 at 09:11:34 UTC, jkpl wrote:
> On Saturday, 2 April 2016 at 08:48:10 UTC, rikki cattermole wrote:
>> On 02/04/2016 9:36 PM, jkpl wrote:
>>> On Saturday, 2 April 2016 at 08:27:07 UTC, rikki cattermole wrote:
>> Okay that is a problem then.
>
> Yes clearly!
>
> Maybe this, a bit better:
>
>
>     foreach (b0; randomCover(iota(0,256)))
>     foreach (b1; randomCover(iota(0,256)))
>     ...
>     foreach (b255; randomCover(iota(0,256)))
>     {
>         ...
>     }

Still not the right approach,


import std.stdio;
import std.random;
import std.range;

void testRunner(bool function(ubyte[128]) test)
{
    uint[32] arr;

    foreach (v0;  randomCover(iota(0U,uint.max)))
    foreach (v1;  randomCover(iota(0U,uint.max)))
    foreach (v2;  randomCover(iota(0U,uint.max)))
    foreach (v3;  randomCover(iota(0U,uint.max)))
    foreach (v4;  randomCover(iota(0U,uint.max)))
    foreach (v5;  randomCover(iota(0U,uint.max)))
    foreach (v6;  randomCover(iota(0U,uint.max)))
    foreach (v7;  randomCover(iota(0U,uint.max)))
    foreach (v8;  randomCover(iota(0U,uint.max)))
    foreach (v9;  randomCover(iota(0U,uint.max)))
    foreach (v10; randomCover(iota(0U,uint.max)))
    foreach (v11; randomCover(iota(0U,uint.max)))
    foreach (v12; randomCover(iota(0U,uint.max)))
    foreach (v13; randomCover(iota(0U,uint.max)))
    foreach (v14; randomCover(iota(0U,uint.max)))
    foreach (v15; randomCover(iota(0U,uint.max)))
    foreach (v16; randomCover(iota(0U,uint.max)))
    foreach (v17; randomCover(iota(0U,uint.max)))
    foreach (v18; randomCover(iota(0U,uint.max)))
    foreach (v19; randomCover(iota(0U,uint.max)))
    foreach (v20; randomCover(iota(0U,uint.max)))
    foreach (v21; randomCover(iota(0U,uint.max)))
    foreach (v22; randomCover(iota(0U,uint.max)))
    foreach (v23; randomCover(iota(0U,uint.max)))
    foreach (v24; randomCover(iota(0U,uint.max)))
    foreach (v25; randomCover(iota(0U,uint.max)))
    foreach (v26; randomCover(iota(0U,uint.max)))
    foreach (v27; randomCover(iota(0U,uint.max)))
    foreach (v28; randomCover(iota(0U,uint.max)))
    foreach (v29; randomCover(iota(0U,uint.max)))
    foreach (v30; randomCover(iota(0U,uint.max)))
    foreach (v31; randomCover(iota(0U,uint.max)))
    {
        arr = [v0,v1,v2,v3,v4,v5,v6,v7,v8,v9,
               v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,
               v20,v21,v22,v23,v24,v25,v26,v27,v28,v29,
               v30, v31
        ];

        writeln(arr);

        //if (test(cast(ubyte[128]) arr))
            return;
    }
}

bool test(ubyte[128] arr)
{
    if (arr[0] == 0U)
        return true;
    else
        return false;
}

void main()
{
    testRunner(&test);
}
April 02, 2016
gives: core.exception.OutOfMemoryError@src/core/exception.d(693): Memory allocation failed



April 02, 2016
[Probably a repost.]

On 04/02/2016 01:20 AM, jkpl wrote:
> Let's say I have a ubyte[256]. I want to test all the possible values
> this array can have on a function.

nextPermutation():

  https://dlang.org/phobos/std_algorithm_sorting.html#nextPermutation

Ali

April 02, 2016
On 4/2/16 5:47 AM, jkpl wrote:
> gives: core.exception.OutOfMemoryError@src/core/exception.d(693): Memory
> allocation failed
>
>
>
Probably because randomCover needs to allocate a bool for all the elements it has already covered, so you are allocating 32 * 4G bools. How much RAM do you have? Note that D's GC is conservative, so false pointers, especially for large arrays also can cause problems.

I'll note that even if it worked, this code will never complete in your lifetime, or even your great great grandchild's lifetime. I don't understand the point of doing this.

I'll also note that if you just want to test a *subset* of all the possible inputs (given the length of time taken to test all these), you probably want to change different elements each time through the loop. Your code spends a long time testing the same elements in the first slots. Given your test code is just looking at the first element, this isn't very good test coverage.

I honestly think you are better off just generating random arrays, even if it results in some overlap (unlikely to be relevant).

-Steve
April 02, 2016
On Saturday, 2 April 2016 at 18:32:03 UTC, Steven Schveighoffer wrote:
> I honestly think you are better off just generating random arrays, even if it results in some overlap (unlikely to be relevant).
>
> -Steve

Yes I know, I've realized how it's silly. just foreach(xn; 0 .. range) foreach(xn; 0 .. range) foreach(xn; 0 .. range) foreach(xn; 0 .. range) is enough. But still silly in term of complexity. I have to go for a heuristic approach. UNtil that I still use a prng.