Browse Source

feat: Adding rand function

Matthias Ladkau 3 years ago
parent
commit
6f8b42c21f

+ 1 - 1
cli/tool/interpret_test.go

@@ -340,7 +340,7 @@ func TestHandleInput(t *testing.T) {
 
 	// Just check for a simple string no need for the whole thing
 
-	if !strings.Contains(testTerm.out.String(), "New creates a new object instance.") {
+	if !strings.Contains(testTerm.out.String(), "Creates a new object instance.") {
 		t.Error("Unexpected result:", testTerm.out.String())
 		return
 	}

+ 5 - 0
engine/pool/threadpool_test.go

@@ -344,6 +344,11 @@ func TestThreadPoolThresholds(t *testing.T) {
 
 	tp.WaitAll()
 
+	if state := fmt.Sprint(tp.State()); state == `` {
+		t.Error("Unexpected state:", state)
+		return
+	}
+
 	if wc := tp.WorkerCount(); wc != 10 {
 		t.Error("Unexpected result:", wc)
 		return

+ 43 - 17
interpreter/func_provider.go

@@ -12,6 +12,7 @@ package interpreter
 
 import (
 	"fmt"
+	"math/rand"
 	"strconv"
 	"strings"
 	"time"
@@ -37,6 +38,7 @@ var InbuildFuncMap = map[string]util.ECALFunction{
 	"add":             &addFunc{&inbuildBaseFunc{}},
 	"concat":          &concatFunc{&inbuildBaseFunc{}},
 	"now":             &nowFunc{&inbuildBaseFunc{}},
+	"rand":            &randFunc{&inbuildBaseFunc{}},
 	"timestamp":       &timestampFunc{&inbuildBaseFunc{}},
 	"dumpenv":         &dumpenvFunc{&inbuildBaseFunc{}},
 	"doc":             &docFunc{&inbuildBaseFunc{}},
@@ -182,7 +184,7 @@ func (rf *rangeFunc) Run(instanceID string, vs parser.Scope, is map[string]inter
 DocString returns a descriptive string.
 */
 func (rf *rangeFunc) DocString() (string, error) {
-	return "Range function which can be used to iterate over number ranges. Parameters are start, end and step.", nil
+	return "Iterates over number ranges. Parameters are start, end and step.", nil
 }
 
 // New
@@ -279,7 +281,7 @@ func (rf *newFunc) addSuperClasses(vs parser.Scope, is map[string]interface{},
 DocString returns a descriptive string.
 */
 func (rf *newFunc) DocString() (string, error) {
-	return "New creates a new object instance.", nil
+	return "Creates a new object instance.", nil
 }
 
 // Type
@@ -312,7 +314,7 @@ func (rf *typeFunc) Run(instanceID string, vs parser.Scope, is map[string]interf
 DocString returns a descriptive string.
 */
 func (rf *typeFunc) DocString() (string, error) {
-	return "Type returns the underlying types and values of an object.", nil
+	return "Returns the underlying types and values of an object.", nil
 }
 
 // Len
@@ -353,7 +355,7 @@ func (rf *lenFunc) Run(instanceID string, vs parser.Scope, is map[string]interfa
 DocString returns a descriptive string.
 */
 func (rf *lenFunc) DocString() (string, error) {
-	return "Len returns the size of a list or map.", nil
+	return "Returns the size of a list or map.", nil
 }
 
 // Del
@@ -400,7 +402,7 @@ func (rf *delFunc) Run(instanceID string, vs parser.Scope, is map[string]interfa
 DocString returns a descriptive string.
 */
 func (rf *delFunc) DocString() (string, error) {
-	return "Del removes an item from a list or map.", nil
+	return "Removes an item from a list or map.", nil
 }
 
 // Add
@@ -447,7 +449,7 @@ func (rf *addFunc) Run(instanceID string, vs parser.Scope, is map[string]interfa
 DocString returns a descriptive string.
 */
 func (rf *addFunc) DocString() (string, error) {
-	return "Add adds an item to a list. The item is added at the optionally given index or at the end if no index is specified.", nil
+	return "Adds an item to a list. The item is added at the optionally given index or at the end if no index is specified.", nil
 }
 
 // Concat
@@ -494,7 +496,7 @@ func (rf *concatFunc) Run(instanceID string, vs parser.Scope, is map[string]inte
 DocString returns a descriptive string.
 */
 func (rf *concatFunc) DocString() (string, error) {
-	return "Concat joins one or more lists together. The result is a new list.", nil
+	return "Joins one or more lists together. The result is a new list.", nil
 }
 
 // dumpenv
@@ -518,7 +520,7 @@ func (rf *dumpenvFunc) Run(instanceID string, vs parser.Scope, is map[string]int
 DocString returns a descriptive string.
 */
 func (rf *dumpenvFunc) DocString() (string, error) {
-	return "Dumpenv returns the current variable environment as a string.", nil
+	return "Returns the current variable environment as a string.", nil
 }
 
 // now
@@ -543,7 +545,31 @@ func (rf *nowFunc) Run(instanceID string, vs parser.Scope, is map[string]interfa
 DocString returns a descriptive string.
 */
 func (rf *nowFunc) DocString() (string, error) {
-	return "Now returns the current time in microseconds from 1st of January 1970 UTC.", nil
+	return "Returns the current time in microseconds from 1st of January 1970 UTC.", nil
+}
+
+// rand
+// ====
+
+/*
+randFunc returns a pseudo-random number between 0 and 1 from the default source.
+*/
+type randFunc struct {
+	*inbuildBaseFunc
+}
+
+/*
+Run executes this function.
+*/
+func (rf *randFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
+	return rand.Float64(), nil
+}
+
+/*
+DocString returns a descriptive string.
+*/
+func (rf *randFunc) DocString() (string, error) {
+	return "Returns a pseudo-random number between 0 and 1 from the default source.", nil
 }
 
 // timestamp
@@ -591,7 +617,7 @@ func (rf *timestampFunc) Run(instanceID string, vs parser.Scope, is map[string]i
 DocString returns a descriptive string.
 */
 func (rf *timestampFunc) DocString() (string, error) {
-	return "Timestamp returns a human readable time stamp string from a given number of microseconds since posix epoch time.", nil
+	return "Returns a human readable time stamp string from a given number of microseconds since posix epoch time.", nil
 }
 
 // doc
@@ -648,7 +674,7 @@ func (rf *docFunc) Run(instanceID string, vs parser.Scope, is map[string]interfa
 DocString returns a descriptive string.
 */
 func (rf *docFunc) DocString() (string, error) {
-	return "Doc returns the docstring of a function.", nil
+	return "Returns the docstring of a function.", nil
 }
 
 // sleep
@@ -685,7 +711,7 @@ func (rf *sleepFunc) Run(instanceID string, vs parser.Scope, is map[string]inter
 DocString returns a descriptive string.
 */
 func (rf *sleepFunc) DocString() (string, error) {
-	return "Sleep pauses the current thread for a number of micro seconds.", nil
+	return "Pauses the current thread for a number of micro seconds.", nil
 }
 
 // raise
@@ -736,7 +762,7 @@ func (rf *raise) Run(instanceID string, vs parser.Scope, is map[string]interface
 DocString returns a descriptive string.
 */
 func (rf *raise) DocString() (string, error) {
-	return "Raise returns an error object.", nil
+	return "Raise an error which stops the execution unless it is handled by a try/except block.", nil
 }
 
 // addEvent
@@ -826,7 +852,7 @@ func (rf *addevent) addEvent(addFunc func(engine.Processor, *engine.Event, *engi
 DocString returns a descriptive string.
 */
 func (rf *addevent) DocString() (string, error) {
-	return "AddEvent adds an event to trigger sinks. This function will return " +
+	return "Adds an event to trigger sinks. This function will return " +
 		"immediately and not wait for the event cascade to finish.", nil
 }
 
@@ -896,7 +922,7 @@ func (rf *addeventandwait) Run(instanceID string, vs parser.Scope, is map[string
 DocString returns a descriptive string.
 */
 func (rf *addeventandwait) DocString() (string, error) {
-	return "AddEventAndWait adds an event to trigger sinks. This function will " +
+	return "Adds an event to trigger sinks. This function will " +
 		"return once the event cascade has finished.", nil
 }
 
@@ -964,7 +990,7 @@ func (ct *setCronTrigger) Run(instanceID string, vs parser.Scope, is map[string]
 DocString returns a descriptive string.
 */
 func (ct *setCronTrigger) DocString() (string, error) {
-	return "setCronTrigger adds a periodic cron job which fires events.", nil
+	return "Adds a periodic cron job which fires events.", nil
 }
 
 // setPulseTrigger
@@ -1040,5 +1066,5 @@ func (pt *setPulseTrigger) Run(instanceID string, vs parser.Scope, is map[string
 DocString returns a descriptive string.
 */
 func (pt *setPulseTrigger) DocString() (string, error) {
-	return "setPulseTrigger adds recurring events in microsecond intervals.", nil
+	return "Adds recurring events in microsecond intervals.", nil
 }

+ 14 - 1
interpreter/func_provider_test.go

@@ -298,6 +298,19 @@ identifier: timestamp
 		return
 	}
 
+	res, err = UnitTestEvalAndAST(
+		`rand()`, nil,
+		`
+identifier: rand
+  funccall
+`[1:])
+	errorutil.AssertOk(err)
+
+	if !strings.Contains(fmt.Sprint(res), ".") {
+		t.Error("Unexpected result: ", res, err)
+		return
+	}
+
 	res, err = UnitTestEval(
 		`
 func foo() {
@@ -315,7 +328,7 @@ doc(foo)`, nil)
 		`doc(len)`, nil)
 	errorutil.AssertOk(err)
 
-	if fmt.Sprint(res) != `Len returns the size of a list or map.` {
+	if fmt.Sprint(res) != `Returns the size of a list or map.` {
 		t.Error("Unexpected result: ", res, err)
 		return
 	}