main.mx raw

   1  // Regression fixture: rune-slice conversions, keyed literals with named
   2  // constant keys, and a func field promoted from an embedded struct. Each of the
   3  // three used to produce a wrong answer or a spurious compile error in stage4
   4  // while legacy was already correct.
   5  package main
   6  
   7  import (
   8  	"os"
   9  	"tests/regressions/should_compile/rune_keyed_promoted/lib"
  10  )
  11  
  12  func bad(msg string) {
  13  	println("FAIL:", msg)
  14  	os.Exit(1)
  15  }
  16  
  17  func main() {
  18  	// Termination: a batch program must still be stoppable, so the entry
  19  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  20  	select {
  21  	case <-shutdown:
  22  		return
  23  	default:
  24  	}
  25  
  26  	// 1. runes to bytes and back
  27  	rs := []rune{'a', 0xfc, 'c'}
  28  	s := lib.RunesToBytes(rs)
  29  	if int32(len(s)) != 4 {
  30  		bad("runes to bytes length " | int32(len(s)).String())
  31  	}
  32  	if string(s) != "aüc" {
  33  		bad("runes to bytes content " | string(s))
  34  	}
  35  	back := lib.BytesToRunes("aüc")
  36  	if int32(len(back)) != 3 {
  37  		bad("bytes to runes length " | int32(len(back)).String())
  38  	}
  39  	if back[0] != 'a' || back[1] != 0xfc || back[2] != 'c' {
  40  		bad("bytes to runes content")
  41  	}
  42  
  43  	// 2. keyed literals with named constant keys
  44  	st := lib.Walk([]uint16{1, 1, 1})
  45  	if st != 2 {
  46  		bad("keyed array walk ended in state " | int32(st).String())
  47  	}
  48  	if v, cnt := lib.KeyedSliceAt(2); v != 40 || cnt != 5 {
  49  		bad("keyed slice: v=" | int32(v).String() | " n=" | int32(cnt).String())
  50  	}
  51  	if v, _ := lib.KeyedSliceAt(4); v != 50 {
  52  		bad("keyed slice last element " | int32(v).String())
  53  	}
  54  	if v, _ := lib.KeyedSliceAt(1); v != 0 {
  55  		bad("keyed slice gap should be zero, got " | int32(v).String())
  56  	}
  57  
  58  	// 3. func field promoted from an embedded struct
  59  	b, got := lib.ThroughField([]byte("ExAmPle"))
  60  	if string(b) != "example" || got != 7 {
  61  		bad("promoted func field: '" | string(b) | "' n=" | int32(got).String())
  62  	}
  63  
  64  	println("return rune_keyed_promoted ok")
  65  }
  66