tarrayboundscheck.nim 736 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. test_arrayboundscheck()
  38. {.pop.}