tarrayboundscheck.nim 906 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. discard """
  2. output: '''idx out of bounds: -1
  3. month out of bounds: 0
  4. Jan
  5. Feb
  6. Mar
  7. Apr
  8. May
  9. Jun
  10. Jul
  11. Aug
  12. Sep
  13. Oct
  14. Nov
  15. Dec
  16. month out of bounds: 13
  17. idx out of bounds: 14
  18. '''
  19. """
  20. {.push boundChecks:on.}
  21. # see issue #6532:
  22. # js backend 0.17.3: array bounds check for non zero based arrays is buggy
  23. proc test_arrayboundscheck() =
  24. var months: array[1..12, string] =
  25. ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  26. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
  27. var indices = [0,1,2,3,4,5,6,7,8,9,10,11,12,13]
  28. for i in -1 .. 14:
  29. try:
  30. let idx = indices[i]
  31. try:
  32. echo months[idx]
  33. except:
  34. echo "month out of bounds: ", idx
  35. except:
  36. echo "idx out of bounds: ", i
  37. # #13966
  38. var negativeIndexed: array[-2..2, int] = [0, 1, 2, 3, 4]
  39. negativeIndexed[-1] = 2
  40. negativeIndexed[1] = 2
  41. doAssert negativeIndexed == [0, 2, 2, 2, 4]
  42. test_arrayboundscheck()
  43. {.pop.}