root/lu/Example.lua

Revision 823 (checked in by rsz, 3 years ago)

cleanup

Line 
1 package.path = package.path .. ";./Library/Lua/?.lua"
2 package.cpath = package.cpath .. ";./Library/Modules/?.so"
3
4 -- import dependencies
5
6 local LUObject = require( "LUObject" )
7 local LUBundle = require( "LUBundle" )
8 local LUString = require( "LUString" )
9 local LUList = require( "LUList" )
10 local LUMap = require( "LUMap" )
11 local MyClass = require( "MyClass" )
12 local MyOtherClass = require( "MyOtherClass" )
13 local ATask = require( "ATask" )
14 local LUTask = require( "LUTask" )
15 local LURunLoop = require( "LURunLoop" )
16 local LUClass = require( "LUClass" )
17 local LUDate = require( "LUDate" )
18 local LULog = require( "LULog" )
19 local LUTimer = require( "LUTimer" )
20 local LUFile = require( "LUFile" )
21 local LFSFile = require( "LFSFile" )
22 local LUConfig = require( "LUConfig" )
23 local DBMap = require( "DBMap" )
24 local LUInputStream = require( "LUInputStream" )
25 local LULocale = require( "LULocale" )
26 local LUModule = require( "LUModule" )
27 local LUCrypto = require( "LUCrypto" )
28 local debug = require( "debug" )
29 local os = require( "os" )
30
31 local anObject = LUObject()
32
33 LULog:debug( LUObject )
34
35 local anotherObject = LUObject()
36
37 LULog:debug( LUObject )
38
39 LULog:debug( anObject )
40 LULog:debug( anotherObject )
41 LULog:debug( anObject:hashCode() )
42 LULog:debug( anotherObject:hashCode() )
43 LULog:debug( anObject == anotherObject )
44
45 LULog:debug( anObject )
46 LULog:debug( type( anObject ) )
47
48 local aList = LUList()
49
50 LULog:debug( "aList.hashCode", aList:hashCode() )
51
52 -- aList:add( anObject );
53 -- aList:add( anotherObject );
54
55 LULog:debug( LUList( { 1, 2, 3 } ):store( LUBundle( LUList ):path() .. "/list.txt" ) )
56
57 aList:add( "aString" );
58 aList:add( true );
59 aList:add( false );
60 aList:add( 10 );
61 aList:add( { 1, 2, 3 } );
62
63 -- LULog:debug( "aList:get", aList:get( anObject ) )
64 -- LULog:debug( "aList:get", aList:get( 1 ) )
65 LULog:debug( aList:get( true ) )
66 LULog:debug( aList:size() )
67 LULog:debug( aList )
68
69 aList:removeValue( anObject )
70
71 LULog:debug( aList:size() )
72
73 aList:removeValue( anotherObject );
74
75 LULog:debug( aList:size() )
76 LULog:debug( aList:equals( aList ) )
77
78 local anotherList = LUList()
79
80 LULog:debug( anotherList:equals( aList ) )
81
82 anotherList:addAll( aList )
83
84 LULog:debug( "anotherList.hashCode", anotherList:hashCode() )
85 LULog:debug( "anotherList.equals", anotherList:equals( aList ) )
86
87 local aPath = LUBundle:bundleWithName( "MyClass" ):path()
88
89 LULog:debug( "aPath",  aPath )
90
91 aList:store( aPath .. "/list.txt" )
92
93 --[[
94 local aTable = { [ 1 ] = false, [ 2 ] = 10, [ 3 ] = { "1" = 1, "3" = 3, "2" = 2 } }
95
96 LULog:debug( "* aTable", aTable )
97
98 local aTable, anError = loadstring( "return { [ 1 ] = false, [ 2 ] = 10, [ 3 ] = { "1" = 1, "3" = 3, "2" = 2 } }" )()
99 LULog:debug( "* aTable loadstring", anError, aTable )
100 --]]
101
102 LULog:debug( aPath )
103
104 local aPersistentList, aStatus = LUList():load( aPath .. "/list.txt" )
105
106 LULog:debug( "aList", aList )
107 LULog:debug( "aPersistentList", aPersistentList )
108 LULog:debug( "aPersistentList.equals", aPersistentList:equals( aList ) )
109
110 local aMap = LUMap()
111
112 LULog:debug( "aMap.hashCode", aMap:hashCode() )
113
114 aMap:put( "aKey", "aValue" )
115 aMap:put( 1, true )
116 aMap:put( 2, false )
117 aMap:put( 3, 4 )
118 aMap:put( true, false )
119 -- aMap:put( anObject, anObject )
120
121 -- aMap:put( anObject, nil )
122 aMap:put( "aList", aList )
123
124 LULog:debug( aMap:size() )
125 LULog:debug( aMap )
126 LULog:debug( aMap:keys() )
127 LULog:debug( aMap:values() )
128
129 aMap:store( aPath .. "/map.txt" )
130
131 LULog:debug( "* " .. aPath )
132
133 local aPersistentMap = LUMap():load( aPath .. "/map.txt" )
134
135
136 LULog:debug( "aMap", aMap )
137 LULog:debug( "aPersistentMap", aPersistentMap )
138 LULog:debug( "aPersistentMap.equals", aPersistentMap:equals( aMap ) )
139
140 local anotherMap = LUMap()
141
142 anotherMap:putAll( aMap )
143 anotherMap:put( "anotherKey", "anotherValue" )
144
145 LULog:debug( anotherMap:keys() )
146 LULog:debug( anotherMap:values() )
147 LULog:debug( anotherMap:equals( aMap ) )
148
149 local myClass = MyClass()
150 local myOtherClass = MyOtherClass()
151
152 myClass:dwim()
153 myOtherClass:dwim()
154
155 LULog:debug( LUMap( _G ):keys():sort() )
156 LULog:debug( LUMap( io ):keys():sort() )
157 LULog:debug( LUMap( arg ) )
158 LULog:debug( LUMap( os ):keys():sort() )
159 LULog:debug( LUMap( math ):keys():sort() )
160 LULog:debug( LUMap( string ):keys():sort() )
161 LULog:debug( LUMap( table ):keys():sort() )
162 LULog:debug( "package", LUMap( package ):keys():sort() )
163 LULog:debug( "package.loaded", LUMap( package[ "loaded" ] ):keys():sort() )
164 LULog:debug( package.path )
165 LULog:debug( package.cpath )
166
167 LULog:debug( myOtherClass )
168 LULog:debug( myOtherClass.isKindOf( myClass ) )
169
170 LULog:debug( LUClass:classNames(), "classNames" )
171 LULog:debug( LUClass:classWithName( "LUObject" ) )
172 LULog:debug( LUClass( LUObject ):methodNames() )
173 LULog:debug( LUClass( LUClass ) )
174 LULog:debug( LUBundle( LUBundle ):path() )
175
176 local aBundle = LUBundle:bundleWithName( "MyClass" )
177
178 LULog:debug( LUBundle:bundleNames(), "bundleNames" )
179
180 LULog:trace( aBundle:fileWithName( "picture", "png", "Images", "en-us" ) )
181 LULog:debug( aBundle:fileWithName( "picture", "png", "Images" ) )
182 LULog:debug( aBundle:fileWithName( "picture", "png" ) )
183 LULog:warning( aBundle:fileWithName( "picture" ) )
184 LULog:error( aBundle:fileWithName() )
185
186 LULog:debug( LUDate() )
187 LULog:debug( LUDate():utcDate() )
188 LULog:debug( LUDate():time() )
189 LULog:debug( LUDate():utc() )
190 LULog:debug( LUDate():date() )
191 LULog:debug( LUDate:timeZone() )
192 LULog:debug( "timeZoneOffset", LUDate:timeZoneOffset() )
193 LULog:debug( LUDate:dateWithValues( { year = 1970, month = 1, day = 1 } ) )
194
195 local someClassNames = LUClass:classNames()
196
197 LULog:debug( someClassNames )
198
199 for anIndex, aClassName in someClassNames:iterator() do
200         local aClass = LUClass:classWithName( aClassName )
201         local anInstance = aClass:entity()()
202        
203         if anInstance ~= nil then
204                 LULog:debug( LUClass:classWithObject( anInstance ) )
205         end
206                
207         LULog:debug( LUClass:classWithObject( aClass:entity()) )
208         LULog:debug( aClass:name() )
209         LULog:debug( aClass:superClass() )
210         LULog:debug( aClass:methodNames() )
211 end
212
213 LULog:debug( os.getenv( "USER" ) )
214
215 require( "posix" )
216
217 LULog:debug( posix.uname() )
218 LULog:debug( posix.getcwd() )
219 LULog:debug( posix.getprocessid() )
220 LULog:debug( os.getenv( "USER" ) )
221 -- LULog:debug( posix.sleep( 1 ) )
222
223 require( "random" )
224
225 local aRandom = random.new( os.time() )
226
227 LULog:debug( aRandom:value(), "random" )
228 LULog:debug( aRandom:value() )
229 LULog:debug( aRandom:value() )
230 LULog:debug( aRandom:value() )
231
232 require( "lfs" )
233
234 LULog:debug( lfs.currentdir() )
235
236 require( "uuid" )
237
238 local anUUID = uuid.new( "time" )
239
240 LULog:debug( "uuid", anUUID, os.date( "%x", uuid.time( anUUID ) ) )
241 LULog:debug( "uuid", uuid.new() )
242 LULog:debug( "uuidmd5", md5.digest( uuid.new() ) )
243
244 require( "md5" )
245
246 LULog:debug( "md5", md5.digest( "1" ) )
247
248 require( "crc" )
249
250 local anHash = crc.calc( "1" )
251
252 LULog:debug( "crc", ( "%x" ):format( crc.calc( "1" ) ) )
253
254 LULog:debug( "random", ( "%x" ):format( random.new( 1 ):value() * 1000000 ) )
255
256 local aFile = LFSFile:currentDirectory()
257
258 LULog:debug( aFile:name() )
259 LULog:debug( aFile:directory() )
260 LULog:debug( aFile:path() )
261 LULog:debug( aFile:exists() )
262 LULog:debug( aFile:isFile() )
263 LULog:debug( aFile:isDirectory() )
264 LULog:debug( aFile:attributes() )
265
266 local aFilter = function( aDirectory, aFileName )
267         if aFileName:find( ".lua" ) ~= nil then
268                 return true
269         end
270        
271         return false
272 end
273
274 LULog:debug( "****" )
275 LULog:debug( aFile:list( aFilter ) )
276 LULog:debug( LFSFile( nil, LUList( aFile:list( aFilter ) ):last() ) )
277 LULog:debug( LFSFile( nil, LUList( aFile:list( aFilter ) ):last() ):size() )
278 LULog:debug( LFSFile( nil, LUList( aFile:list( aFilter ) ):last() ):attributes() )
279 LULog:debug( aFile:path() )
280 LULog:debug( aFile:pathComponents() )
281 LULog:debug( aFile:parentDirectory() )
282 LULog:debug( aFile:isAbsolute() )
283
284 while true do
285        
286         LULog:debug( aFile )
287        
288         if aFile:parentDirectory() ~= nil then
289                 aFile = LUFile( aFile:parentDirectory() )
290         else
291                 break
292         end
293 end
294
295 LULog:debug( LUFile:tmpDir() )
296 LULog:debug( LUFile:tmpName( "prefix", "suffix" ) )
297 LULog:debug( LUFile:tmpName( "prefix" ) )
298 LULog:debug( LUFile:tmpName( nil, "suffix" ) )
299 LULog:debug( LUFile:tmpName() )
300 LULog:debug( LUFile:tmpFile() )
301 LULog:debug( LUFile:tmpFile():isAbsolute() )
302
303 LULog:debug( LUConfig:name() )
304 LULog:debug( LUConfig:path() )
305 LULog:debug( LUFile( LUConfig:path() ):isAbsolute() )
306 LULog:debug( LUConfig:configWithName() )
307
308 local aDBMap = DBMap():load( "./gdbm.db" )
309
310 aDBMap:putAll( { [ "AUG" ] = "August", [ "SEP" ] = "September" } )
311 aDBMap:put( "OCT", "October" )
312 aDBMap:put( "NOV", "November" )
313 aDBMap:put( "DEC", "December" )
314
315 LULog:debug( aDBMap )
316 LULog:debug( aDBMap:path() )
317 LULog:debug( aDBMap:db() )
318 LULog:debug( aDBMap:size() )
319 LULog:debug( aDBMap:keys():sort() )
320 LULog:debug( aDBMap:values():sort() )
321 LULog:debug( aDBMap:get( "DEC" ) )
322 LULog:debug( aDBMap:hasKey( "DEC" ) )
323 LULog:debug( aDBMap:hasValue( "December" ) )
324
325
326 aDBMap:store()
327 aDBMap:optimize()
328 aDBMap:close()
329
330 local aClass, aStatus = LUClass:classWithName( "asdfasdfasd" )
331
332 LULog:debug( aClass )
333 LULog:debug( aStatus )
334
335 local aList = LUList( { 1, 2, 3, 4, 5, 6, 7, 8, 9 } )
336
337 LULog:debug( aList )
338 LULog:debug( aList:reverse() )
339 LULog:debug( aList:reverse() )
340
341 LULog:debug( aList:having( function( aValue ) if aValue < 5 then return true end return false end ) )
342 LULog:debug( aList:range( 5, 9 ) )
343 LULog:debug( 1, aList:sort():shuffle() )
344 LULog:debug( 2, aList:sort():shuffle() )
345 LULog:debug( 3, aList:sort():shuffle() )
346 LULog:debug( 4, aList:sort():shuffle() )
347 LULog:debug( 5, aList:sort():shuffle() )
348 LULog:debug( 6, aList:sort():shuffle() )
349 LULog:debug( 7, aList:sort():shuffle() )
350 LULog:debug( 8, aList:sort():shuffle() )
351 LULog:debug( 9, aList:sort():shuffle() )
352 LULog:debug( aList:sort() )
353
354 local anInputStream = LUInputStream( "abc-def" )
355
356 for aValue in anInputStream:iterator( true ) do
357         LULog:debug( "read", ( "%q" ):format( aValue ) )
358         LULog:debug( "string", ( "%q" ):format( anInputStream:toString() ) )
359 end
360
361 LULog:debug( LULocale:default() )
362 LULog:debug( LULocale:default():countryName() )
363 LULog:debug( LULocale:default():languageName() )
364 LULog:debug( LULocale:default():currency() )
365 LULog:debug( LULocale:default():currencyName() )
366 LULog:debug( LULocale:default():days() )
367 LULog:debug( LULocale:default():shortDays() )
368 LULog:debug( LULocale:default():months() )
369 LULog:debug( LULocale:default():shortMonths() )
370 LULog:debug( LULocale:default():areas() )
371 LULog:debug( LULocale:default():locations() )
372 LULog:debug( LULocale:default():zones() )
373 LULog:debug( "allCountries", LULocale:allCountries():size() )
374 LULog:debug( "allCurrencies", LULocale:allCurrencies():size() )
375 LULog:debug( "allLanguages", LULocale:allLanguages():size() )
376 LULog:debug( "allZones", LULocale:allZones():size() )
377
378 local someModules = LUModule:moduleNames()
379
380 for anIndex, aName in someModules:iterator() do
381         local aModule = LUModule:moduleWithName( aName )
382        
383         LULog:debug( aModule, aModule:version() )
384         LULog:debug( aModule:functionNames() )
385         LULog:debug( aModule:attributeNames() )
386 end
387
388 LULog:debug( LUModule:moduleWithName( "_G" ), LUModule:moduleWithName( "_G" ):version() )
389 LULog:debug( LUModule:moduleWithName( "_G" ):functionNames() )
390 LULog:debug( LUModule:moduleWithName( "_G" ):attributeNames() )
391
392 local zlib = require( "zlib" )
393
394 local aCompressedValue = zlib.compress( "hello! hello! hello!" )
395
396 LULog:debug( "zlib", LUString:encode( aCompressedValue ) )
397
398 local anUncompressedValue = zlib.decompress( aCompressedValue )
399
400 LULog:debug( "zlib", anUncompressedValue )
401
402 LUTimer( ATask(), 1, true ):start()
403 LUTimer( ATask(), 2, true ):start()
404
405 -- LURunLoop:default():start()
406
407 require( "crypto" )
408
409 local aFish = crypto.blowfish( "password" )
410 local aString = "secret"
411 local anEncryptedString = aFish( aString )
412 local aDecryptedString = aFish( anEncryptedString, true )
413
414 LULog:debug( aString, LUString:encode( anEncryptedString ), aDecryptedString )
415
416 local aKey = md5.digest( random.new( os.time() ):value() )
417 local anUUID = uuid.new()
418 local aFish = crypto.blowfish( aKey )
419 local aString = aFish( anUUID )
420
421 LULog:debug( aKey, anUUID, LUString:encode( aString ) )
422 LULog:debug( aString == LUString:decode( LUString:encode( aString ) ) )
423
424 LULog:debug( LUCrypto:value() )
425 LULog:debug( uuid.new( "random" ))
426 LULog:debug( uuid.new( "time" ))
427 LULog:debug( LUFile:tmpName() )
428 LULog:debug( collectgarbage( "count" ) )
429 LULog:debug( {} )
430
431 aKey = LUCrypto:key()
432 aString = LUCrypto:key()
433
434 LULog:debug( aKey:len(), aKey )
435
436 local aSignature = LUCrypto:sign( aKey, aString )
437
438 LULog:debug( aSignature:len(), aSignature, LUCrypto:verify( aKey, aString, aSignature ) )
439
Note: See TracBrowser for help on using the browser.