-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.mo
90 lines (66 loc) · 2.67 KB
/
test.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import Array "mo:base/Array";
import Debug "mo:base/Debug";
import IC "mo:base/ExperimentalInternetComputer";
import Nat32 "mo:base/Nat32";
import Prim "mo:prim";
import { time } "mo:prim";
import { sortBool; sortBoolDesc; sortFloat; sortFloatDesc } "./src/sort";
import { sortInt8; sortInt8Desc; sortInt16; sortInt16Desc; sortInt32; sortInt32Desc; sortInt64; sortInt64Desc } "./src/sort";
import { sortNat8; sortNat8Desc; sortNat16; sortNat16Desc; sortNat32; sortNat32Desc; sortNat64; sortNat64Desc } "./src/sort";
actor Test {
func randomNat64(key: Nat64): Nat64 {
var hash = key;
hash := hash >> 30 ^ hash *% 0xbf58476d1ce4e5b9;
hash := hash >> 27 ^ hash *% 0x94d049bb133111eb;
return hash >> 31 ^ hash;
};
func randomNat32(key: Nat64): Nat32 {
return Prim.intToNat32Wrap(Prim.nat64ToNat(randomNat64(key) & 0xffffffff));
};
func randomNat16(key: Nat64): Nat16 {
return Prim.intToNat16Wrap(Prim.nat64ToNat(randomNat64(key) & 0xffff));
};
func randomNat8(key: Nat64): Nat8 {
return Prim.intToNat8Wrap(Prim.nat64ToNat(randomNat64(key) & 0xff));
};
func randomInt64(key: Nat64): Int64 {
return Prim.nat64ToInt64(randomNat64(key));
};
func randomInt32(key: Nat64): Int32 {
return Prim.nat32ToInt32(Prim.intToNat32Wrap(Prim.nat64ToNat(randomNat64(key) & 0xffffffff)));
};
func randomInt16(key: Nat64): Int16 {
return Prim.nat16ToInt16(Prim.intToNat16Wrap(Prim.nat64ToNat(randomNat64(key) & 0xffff)));
};
func randomInt8(key: Nat64): Int8 {
return Prim.nat8ToInt8(Prim.intToNat8Wrap(Prim.nat64ToNat(randomNat64(key) & 0xff)));
};
func randomFloat(key: Nat64): Float {
return Prim.intToFloat(Prim.nat64ToNat(randomNat64(key)));
};
func randomBool(key: Nat64): Bool {
return if (randomNat64(key) & 1 == 1) true else false;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public query func test(): async Text {
var randomSeed = 1:Nat64;
let array = Array.init<Nat32>(100000, 0);
for (i in array.keys()) {
array[i] := randomNat32(randomSeed);
randomSeed +%= 1;
};
var heap = Prim.rts_heap_size();
let cost = IC.countInstructions(func() {
//Array.sortInPlace(array, Nat32.compare);
sortNat32<Nat32>(array, func(item) = item);
});
heap := Prim.rts_heap_size() - heap;
let iter = array.keys();
var errorsCount = 0:Nat32;
ignore iter.next();
for (i in iter) {
if (array[i - 1] > array[i]) errorsCount +%= 1;
};
return "cost - " # debug_show(cost) # ", heap - " # debug_show(heap) # ", errors - " # debug_show(errorsCount);
};
};