Example #1
0
func main() {
	//println("Start test running in: " + tardisgolib.Platform())
	testManyGoroutines()
	testChanSelect()
	tourfib()
	testCaseSensitivity()
	testInit()
	testConst()
	testUTF()
	testFloat()
	testMultiRet()
	testAppend()
	testStruct()
	testHeader()
	testCopy()
	testInFuncPtr()
	testCallBy()
	testMap()
	testNamed()
	testFuncPtr()
	testIntOverflow()
	testSlices()
	testChan()
	testComplex()
	testUTF8()
	testString()
	testClosure()
	testVariadic(42)
	testVariadic(40, 2)
	testVariadic(42, -5, 3, 2)
	testInterface()
	testInterfaceMethods()
	testStrconv()
	testTour64()
	testUintDiv32()
	testUintDiv64()
	testDefer()
	aGrWG.Wait()
	TEQint32(tardisgolib.CPos()+" testManyGoroutines() sync/atomic counter:", aGrCtr, 0)
	if tardisgolib.Host() == "haxe" {
		TEQ(tardisgolib.CPos(), int(tardisgolib.HAXE("42;")), int(42))
		TEQ(tardisgolib.CPos(), string(tardisgolib.HAXE("'test';")), "test")
		TEQ(tardisgolib.CPos()+"Num Haxe GR post-wait", tardisgolib.NumGoroutine(), 1)
	} else {
		TEQ(tardisgolib.CPos()+"Num Haxe GR post-wait", tardisgolib.NumGoroutine(), 2)
	}
	//println("End test running in: " + tardisgolib.Platform())
	//println("再见!Previous two chinese characters should say goodbye! (testing unicode output)")
	//println()
}
Example #2
0
func pushBooks(x, y *float64, state *int, cartLoad int) {
	*state = Full
	for *x = 0.0; *x < 150.0; (*x) += 10.0 / float64(cartLoad) {
		if *y > 0.0 { // create bumps in the road
			*y = 0.0
		} else {
			*y = float64(tardisgolib.HAXE("Std.random(3);")) // random small bumps
		}
		tardisgolib.Gosched() // without this, the animation would not show each state
	}
	if *x > 150.0 { // constrain large x offsets
		*x = 150.0
	}
	*y = 0.0
}
Example #3
0
func moreBooks(x, y *float64, state *int) {
	*state = Empty
	for *x > 0.0 {
		*x -= 10.0
		if *x < 0.0 { // no -ve x offsets please
			*x = 0.0
		}
		if *y > 0.0 { // create bumps in the road
			*y = 0.0
		} else {
			*y = float64(tardisgolib.HAXE("Std.random(5);")) // random bigger bumps
		}
		tardisgolib.Gosched() // would not show state without this, the animation would jump.
	}
	*y = 0.0
}
Example #4
0
func main() {

	// As before we'll count how many operations we perform.
	var ops int64

	// The `reads` and `writes` channels will be used by
	// other goroutines to issue read and write requests,
	// respectively.
	reads := make(chan *readOp)
	writes := make(chan *writeOp)

	// Here is the goroutine that owns the `state`, which
	// is a map as in the previous example but now private
	// to the stateful goroutine. This goroutine repeatedly
	// selects on the `reads` and `writes` channels,
	// responding to requests as they arrive. A response
	// is executed by first performing the requested
	// operation and then sending a value on the response
	// channel `resp` to indicate success (and the desired
	// value in the case of `reads`).
	go func() {
		state := make(map[int]int)
		for {
			select {
			case read := <-reads:
				read.resp <- state[read.key]
			case write := <-writes:
				state[write.key] = write.val
				write.resp <- true
			}
		}
	}()

	// This starts 100 goroutines to issue reads to the
	// state-owning goroutine via the `reads` channel.
	// Each read requires constructing a `readOp`, sending
	// it over the `reads` channel, and the receiving the
	// result over the provided `resp` channel.
	for r := 0; r < 100; r++ {
		go func() {
			for {
				read := &readOp{
					key:  int(tardisgolib.HAXE("Std.random(5);")), // rand.Intn(5),
					resp: make(chan int)}
				reads <- read
				<-read.resp
				atomic.AddInt64(&ops, 1)
			}
		}()
	}

	// We start 10 writes as well, using a similar
	// approach.
	for w := 0; w < 10; w++ {
		go func() {
			for {
				write := &writeOp{
					key:  int(tardisgolib.HAXE("Std.random(5);")),   // rand.Intn(5),
					val:  int(tardisgolib.HAXE("Std.random(100);")), // rand.Intn(100),
					resp: make(chan bool)}
				writes <- write
				<-write.resp
				atomic.AddInt64(&ops, 1)
			}
		}()
	}

	// Let the goroutines work for a second.
	//time.Sleep(time.Second)
	for i := 0; i < 1000; i++ {
		tardisgolib.Gosched()
	}

	// Finally, capture and report the `ops` count.
	opsFinal := atomic.LoadInt64(&ops)
	//fmt.Println("ops:", opsFinal)
	println("ops:", int(opsFinal)) // TODO println of a 64-bit int does not show the actual value on all platforms, only the type name see issue #18
}
Example #5
0
// Provided by package runtime.
func now() (sec int64, nsec int32) {
	return int64(tardisgolib.HAXE("GOint64.ofFloat(Date.now().getTime()/1000.0);")),
		int32(tardisgolib.HAXE("cast(Date.now().getTime()%1000.0,Int)*1000000;"))
}