cx icon indicating copy to clipboard operation
cx copied to clipboard

len() of struct slice returns incorrect result.

Open corpusc opened this issue 4 years ago • 9 comments

Describe ...and it will give different numbers, affected by the values of a struct field.

To Reproduce

  1. 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)) }

  1. 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

corpusc avatar Dec 11 '20 03:12 corpusc

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)) }

SkycoinSynth avatar Feb 04 '21 07:02 SkycoinSynth

arfan@Arfans-MBP examples % cx test.cx len 1680154734 len 622882405

arfan499 avatar Feb 04 '21 08:02 arfan499

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)) }`

SkycoinSynth avatar Feb 04 '21 08:02 SkycoinSynth

len 1680154734 len 622882405

arfan499 avatar Feb 04 '21 08:02 arfan499

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)) }`

SkycoinSynth avatar Feb 04 '21 09:02 SkycoinSynth

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

arfan499 avatar Feb 04 '21 09:02 arfan499

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?

SkycoinSynth avatar Feb 23 '21 15:02 SkycoinSynth

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)) }`

arfan499 avatar Feb 24 '21 07:02 arfan499

test cases added for issue #177 https://github.com/skycoin/cx/tree/develop/tests/issue-177

PratikDhanave avatar Feb 24 '21 12:02 PratikDhanave