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. 231675 function random_choice(arr) {
40. 231675 return arr[Math.random() * arr.length | 0];
41. 231675 }
42. 1
43. 161597 function random_utf8() {
44. 161597 var first = random_choice(first_values);
45. 161597
46. 109484 if (first < 0x80) {
47. 109484 return String.fromCharCode(first);
48. 109484 } else if (first < 0xe0) {
49. 52113 return String.fromCharCode((first & 0x1f) << 0x6 | random_choice(trailing_values) & 0x3f);
50. 52113 } else if (first == 0xe0) {
51. 52113 return String.fromCharCode(((first & 0xf) << 0xc) | ((random_choice(subranges[0]) & 0x3f) << 6) | random_choice(trailing_values) & 0x3f);
52. 52113 } else if (first == 0xed) {
53. 52113 return String.fromCharCode(((first & 0xf) << 0xc) | ((random_choice(subranges[1]) & 0x3f) << 6) | random_choice(trailing_values) & 0x3f);
54. 52113 } else if (first < 0xf0) {
55. 52113 return String.fromCharCode(((first & 0xf) << 0xc) | ((random_choice(trailing_values) & 0x3f) << 6) | random_choice(trailing_values) & 0x3f);
56. 52113 }
57. 161597 }
58. 1
59. 1115 function randomString() {
60. 1115 var str = '',
61. 1115 i;
62. 1115
63. 161597 for (i = Math.random() * 300; i > 0; i--) {
64. 161597 str += random_utf8();
65. 161597 }
66. 1115
67. 1115 return str;
68. 1115 }
69. 1
70. 1
71. 1 // Generate random data.
72. 1 var data = [];
73. 1 var length = Math.floor(Math.random() * 1000) + 200;
74. 1115 for (var i = 0; i < length; i++) {
75. 1115 data.push(randomString());
76. 1115 }
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. 1115 for (var i = 0; i < data.length; i++) {
88. 1115 stmt.run(i, data[i], function(err) {
89. 0 if (err) throw err;
90. 1115 inserted++;
91. 1115 });
92. 1115 }
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. 1115 for (var i = 0; i < rows.length; i++) {
101. 1115 assert.equal(rows[i].txt, data[i]);
102. 1115 retrieved++;
103. 1115 }
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