tsortdev.nim 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. discard """
  2. output: "done tsortdev"
  3. """
  4. import algorithm, strutils
  5. proc cmpPlatforms(a, b: string): int =
  6. if a == b: return 0
  7. var dashes = a.split('-')
  8. var dashes2 = b.split('-')
  9. if dashes[0] == dashes2[0]:
  10. if dashes[1] == dashes2[1]: return system.cmp(a,b)
  11. case dashes[1]
  12. of "x86":
  13. return 1
  14. of "x86_64":
  15. if dashes2[1] == "x86": return -1
  16. else: return 1
  17. of "ppc64":
  18. if dashes2[1] == "x86" or dashes2[1] == "x86_64": return -1
  19. else: return 1
  20. else:
  21. return system.cmp(dashes[1], dashes2[1])
  22. else:
  23. case dashes[0]
  24. of "linux":
  25. return 1
  26. of "windows":
  27. if dashes2[0] == "linux": return -1
  28. else: return 1
  29. of "macosx":
  30. if dashes2[0] == "linux" or dashes2[0] == "windows": return -1
  31. else: return 1
  32. else:
  33. if dashes2[0] == "linux" or dashes2[0] == "windows" or
  34. dashes2[0] == "macosx": return -1
  35. else:
  36. return system.cmp(a, b)
  37. proc sorted[T](a: openArray[T]): bool =
  38. result = true
  39. for i in 0 ..< a.high:
  40. if cmpPlatforms(a[i], a[i+1]) > 0:
  41. echo "Out of order: ", a[i], " ", a[i+1]
  42. result = false
  43. proc main() =
  44. var testData = @["netbsd-x86_64", "windows-x86", "linux-x86_64", "linux-x86",
  45. "linux-ppc64", "macosx-x86-1058", "macosx-x86-1068"]
  46. sort(testData, cmpPlatforms)
  47. doAssert sorted(testData)
  48. for i in 0..1_000:
  49. main()
  50. echo "done tsortdev"