cx
cx copied to clipboard
len() of struct slice returns incorrect result.
Describe ...and it will give different numbers, affected by the values of a struct field.
To Reproduce
- use this code:
package main
type TextInfo struct { Text str Font str Size f32 Wid f32 // caches string measure queries Hei f32 }
func main() { var texts []TextInfo texts = []TextInfo{TextInfo{Text:"hmm"}} printf("len %d \n", len(texts)) }
- Now add an extra 'm' character to the "Text" field, and watch CX report a different size
Desktop:
OS: Windows 10 CX Version 0.7.0 full release
package main
type TextInfo struct { Text str Font str Size f32 Wid f32 // caches string measure queries Hei f32 }
func main() { var texts []TextInfo texts = []TextInfo{TextInfo{Text:"hmm"}} printf("len %d \n", len(texts)) texts = []TextInfo{TextInfo{Text:"hmmm"}} printf("len %d \n", len(texts)) }
arfan@Arfans-MBP examples % cx test.cx len 1680154734 len 622882405
Try this one
`package main
type TestStruct struct { Text str }
func main() { var texts []TestStruct texts = []TestStruct{TestStruct{Text:"hmm"}} printf("len %d \n", len(texts)) texts = []TestStruct{TestStruct{Text:"hmmm"}} printf("len %d \n", len(texts)) }`
len 1680154734 len 622882405
type TestStruct struct { Value int32 }
func main() { var texts []TestStruct texts = []TestStruct{TestStruct{Value: 1}} printf("len %d \n", len(texts)) texts = []TestStruct{TestStruct{Value: 2}} printf("len %d \n", len(texts)) }`
After changing the type of the element of struct from str to i32 the results are arfan@Arfans-MBP examples % cx test.cx len 0 len 0
After changing the type of the element of struct from str to i32 the results are arfan@Arfans-MBP examples % cx test.cx len 0
So bug only occurs for string type and not for int32?
Do we have a unit test, where it uses a string and it fails?
In this unit test, the str length fails
`package main
type TestStruct struct { Text str }
func main() { var texts []TestStruct texts = []TestStruct{TestStruct{Text:"hmm"}} printf("len %d \n", len(texts)) texts = []TestStruct{TestStruct{Text:"hmmm"}} printf("len %d \n", len(texts)) }`
and in this unit test, i32 doesn't fail
type TestStruct struct { Value int32 }
func main() { var texts []TestStruct texts = []TestStruct{TestStruct{Value: 1}} printf("len %d \n", len(texts)) texts = []TestStruct{TestStruct{Value: 2}} printf("len %d \n", len(texts)) }`
test cases added for issue #177 https://github.com/skycoin/cx/tree/develop/tests/issue-177