1. 1var sqlite3 = require('..');
2. 1var assert = require('assert');
3. 1
4. 1describe('unicode', function() {
5. 1 var first_values = [],
6. 1 trailing_values = [],
7. 1 chars = [],
8. 1 subranges = new Array(2),
9. 1 len = subranges.length,
10. 1 db,
11. 1 i;
12. 1
13. 1 before(function(done) { db = new sqlite3.Database(':memory:', done); });
14. 1
15. 96 for (i = 0x20; i < 0x80; i++) {
16. 96 first_values.push(i);
17. 96 }
18. 1
19. 46 for (i = 0xc2; i < 0xf0; i++) {
20. 46 first_values.push(i);
21. 46 }
22. 1
23. 64 for (i = 0x80; i < 0xc0; i++) {
24. 64 trailing_values.push(i);
25. 64 }
26. 1
27. 2 for (i = 0; i < len; i++) {
28. 2 subranges[i] = [];
29. 2 }
30. 1
31. 32 for (i = 0xa0; i < 0xc0; i++) {
32. 32 subranges[0].push(i);
33. 32 }
34. 1
35. 32 for (i = 0x80; i < 0xa0; i++) {
36. 32 subranges[1].push(i);
37. 32 }
38. 1
39. 107321 function random_choice(arr) {
40. 107321 return arr[Math.random() * arr.length | 0];
41. 107321 }
42. 1
43. 74740 function random_utf8() {
44. 74740 var first = random_choice(first_values);
45. 74740
46. 50459 if (first < 0x80) {
47. 50459 return String.fromCharCode(first);
48. 50459 } else if (first < 0xe0) {
49. 24281 return String.fromCharCode((first & 0x1f) << 0x6 | random_choice(trailing_values) & 0x3f);
50. 24281 } else if (first == 0xe0) {
51. 24281 return String.fromCharCode(((first & 0xf) << 0xc) | ((random_choice(subranges[0]) & 0x3f) << 6) | random_choice(trailing_values) & 0x3f);
52. 24281 } else if (first == 0xed) {
53. 24281 return String.fromCharCode(((first & 0xf) << 0xc) | ((random_choice(subranges[1]) & 0x3f) << 6) | random_choice(trailing_values) & 0x3f);
54. 24281 } else if (first < 0xf0) {
55. 24281 return String.fromCharCode(((first & 0xf) << 0xc) | ((random_choice(trailing_values) & 0x3f) << 6) | random_choice(trailing_values) & 0x3f);
56. 24281 }
57. 74740 }
58. 1
59. 509 function randomString() {
60. 509 var str = '',
61. 509 i;
62. 509
63. 74740 for (i = Math.random() * 300; i > 0; i--) {
64. 74740 str += random_utf8();
65. 74740 }
66. 509
67. 509 return str;
68. 509 }
69. 1
70. 1
71. 1 // Generate random data.
72. 1 var data = [];
73. 1 var length = Math.floor(Math.random() * 1000) + 200;
74. 509 for (var i = 0; i < length; i++) {
75. 509 data.push(randomString());
76. 509 }
77. 1
78. 1 var inserted = 0;
79. 1 var retrieved = 0;
80. 1
81. 1 it('should create the table', function(done) {
82. 1 db.run("CREATE TABLE foo (id int, txt text)", done);
83. 1 });
84. 1
85. 1 it('should insert all values', function(done) {
86. 1 var stmt = db.prepare("INSERT INTO foo VALUES(?, ?)");
87. 509 for (var i = 0; i < data.length; i++) {
88. 509 stmt.run(i, data[i], function(err) {
89. 0 if (err) throw err;
90. 509 inserted++;
91. 509 });
92. 509 }
93. 1 stmt.finalize(done);
94. 1 });
95. 1
96. 1 it('should retrieve all values', function(done) {
97. 1 db.all("SELECT txt FROM foo ORDER BY id", function(err, rows) {
98. 0 if (err) throw err;
99. 1
100. 509 for (var i = 0; i < rows.length; i++) {
101. 509 assert.equal(rows[i].txt, data[i]);
102. 509 retrieved++;
103. 509 }
104. 1 done();
105. 1 });
106. 1 });
107. 1
108. 1 it('should have inserted and retrieved the correct amount', function() {
109. 1 assert.equal(inserted, length);
110. 1 assert.equal(retrieved, length);
111. 1 });
112. 1
113. 1 after(function(done) { db.close(done); });
114. 1});
115. 1