digitalmars.D.learn - Idiomatic way to generate all possible values a static array of ubyte
- jkpl (10/10) Apr 02 2016 Let's say I have a ubyte[256]. I want to test all the possible
- rikki cattermole (5/14) Apr 02 2016 Actually I'd use a hex string.
- jkpl (5/13) Apr 02 2016 No I don't get the idea. I think that I haven't well explained.
- rikki cattermole (4/18) Apr 02 2016 Okay that is a problem then.
- jkpl (10/14) Apr 02 2016 Yes clearly!
- jkpl (62/77) Apr 02 2016 Still not the right approach,
- jkpl (2/2) Apr 02 2016 gives: core.exception.OutOfMemoryError@src/core/exception.d(693):
- Steven Schveighoffer (17/19) Apr 02 2016 Probably because randomCover needs to allocate a bool for all the
- jkpl (7/11) Apr 02 2016 Yes I know, I've realized how it's silly. just foreach(xn; 0 ..
- =?UTF-8?Q?Ali_=c3=87ehreli?= (5/7) Apr 02 2016 nextPermutation():
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) ?
Apr 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.
Apr 02 2016
On Saturday, 2 April 2016 at 08:27:07 UTC, rikki cattermole wrote:On 02/04/2016 9:20 PM, jkpl wrote: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.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.
Apr 02 2016
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. Stick with generating it I think, can't hard code that number of combinations.On 02/04/2016 9:20 PM, jkpl wrote: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.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.
Apr 02 2016
On Saturday, 2 April 2016 at 08:48:10 UTC, rikki cattermole wrote:On 02/04/2016 9:36 PM, jkpl wrote: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))) { ... }On Saturday, 2 April 2016 at 08:27:07 UTC, rikki cattermole wrote:Okay that is a problem then.
Apr 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: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); }On 02/04/2016 9:36 PM, jkpl wrote: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))) { ... }On Saturday, 2 April 2016 at 08:27:07 UTC, rikki cattermole wrote:Okay that is a problem then.
Apr 02 2016
gives: core.exception.OutOfMemoryError src/core/exception.d(693): Memory allocation failed
Apr 02 2016
On 4/2/16 5:47 AM, jkpl wrote:gives: core.exception.OutOfMemoryError src/core/exception.d(693): Memory allocation failedProbably 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
Apr 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). -SteveYes 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.
Apr 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
Apr 02 2016