From 75a98ba0671264917a74168f4f5287d82fc8ea11 Mon Sep 17 00:00:00 2001 From: Ge Wang Date: Thu, 31 Oct 2024 23:08:52 -0700 Subject: [PATCH] add unit test 01-Basic #256 --- src/test/01-Basic/256-type-type.ck | 129 +++++++++++++++++++++++++ src/test/01-Basic/256-type-type.txt | 144 ++++++++++++++++++++++++++++ 2 files changed, 273 insertions(+) create mode 100644 src/test/01-Basic/256-type-type.ck create mode 100644 src/test/01-Basic/256-type-type.txt diff --git a/src/test/01-Basic/256-type-type.ck b/src/test/01-Basic/256-type-type.ck new file mode 100644 index 000000000..db57dfac9 --- /dev/null +++ b/src/test/01-Basic/256-type-type.ck @@ -0,0 +1,129 @@ +//--------------------------------------------------------------------- +// name: type-type.ck +// desc: this example shows working with the Type type in ChucK; +// a Type instance represents a specific type currently in +// in the ChucK VM +// +// version: needs 1.5.0.0 or higher +// +// author: Ge Wang (https://ccrma.stanford.edu/~ge/) +// date: Winter 2023 +//--------------------------------------------------------------------- + +// print some info about a Type +fun void about( Type t ) +{ + // divider + cherr <= "--------------------------------------------------" <= IO.newline(); + // check + if( t == null ) + { + cherr <= "null Type passed to about()!" <= IO.newline(); + return; + } + + //----------------------------------------------------------------- + // use .name() to get full type name e.g., 'int[][]' + //----------------------------------------------------------------- + cherr <= "type name using .name(): " <= t.name() <= IO.newline(); + //----------------------------------------------------------------- + // use .baseName() to get base type name (without the array part) + //----------------------------------------------------------------- + cherr <= "type name using .baseName(): " <= t.baseName(); + repeat( t.arrayDepth() ) cherr <= "[]"; + cherr <= IO.newline(); + + //----------------------------------------------------------------- + // parents + //----------------------------------------------------------------- + cherr <= "inherits: "; + // get parent + t.parent() @=> Type @ p; + // nothing + if( p == null ) cherr <= "(nothing)" <= IO.newline(); + else + { + while( p != null ) + { + // name of parent + cherr <= p.name(); + // get parent's parent + p.parent() @=> p; + // null? + cherr <= ((p != null) ? " -> " : IO.newline()); + } + } + + //----------------------------------------------------------------- + // is a kind of? + //----------------------------------------------------------------- + cherr <= "isa...Object:" <= t.isa("Object") <= " " + <= "UGen:" <= t.isa("UGen") <= " " + <= "UAna:" <= t.isa("UAna") <= IO.newline(); + + //----------------------------------------------------------------- + // more attributes + //----------------------------------------------------------------- + cherr <= "primitive: " <= (t.isPrimitive() ? "YES" : "NO" ) <= IO.newline(); + cherr <= "array: " <= (t.isArray() ? "YES" : "NO" ) + <= " depth: " <= t.arrayDepth() <= IO.newline(); +} + +// make an array +int array[2][2]; +about( array.typeOf() ); +about( Type.of( array ) ); + +// a patch +SinOsc foo; + +// get the type of classes +SinOsc.typeOf() @=> Type @ tSinOsc; +// print info +about( tSinOsc ); +// get the type of an instance +foo.typeOf() @=> Type @ tfoo; +// print info (should be the same as above) +about( tfoo ); + +// get type of any value or variable +about( Type.of( 1 ) ); +about( Type.of( 2.0 ) ); +about( Type.of( now ) ); +about( Type.of( 3::second ) ); +about( Type.of( #(1,2) ) ); +about( Type.of( %(1,pi/2) ) ); +about( Type.of( @(1,2,3) ) ); +about( Type.of( @(1,2,3,4) ) ); +about( Type.of( dac ) ); + +// get type by name +about( Type.find("MFCC") ); + +// get the type of the Type type; should be itself +Type.typeOf() @=> Type @ tType; + +// get all subclasses of StkInstrument +StkInstrument.typeOf().children() @=> Type kids[]; +// print results +for( int i; i < kids.size(); i++ ) +{ + cherr <= "StkInstrument: " <= kids[i].name() <= IO.newline(); +} + +// get all subclasses of UAna +UAna.typeOf().children() @=> kids; +// print results +for( int i; i < kids.size(); i++ ) +{ + cherr <= "UAna: " <= kids[i].name() <= IO.newline(); +} + +// instantiate a SinOsc; assign reference to a parent class +SinOsc theChild @=> UGen @ theParent; +// static typing: should be `SinOsc UGen` +<<< theChild.typeOf().name(), + theParent.typeOf().name() >>>; +// instanced typing: should be `SinOsc SinOsc` +<<< theChild.typeOfInstance().name(), + theParent.typeOfInstance().name() >>>; diff --git a/src/test/01-Basic/256-type-type.txt b/src/test/01-Basic/256-type-type.txt new file mode 100644 index 000000000..7ccce6833 --- /dev/null +++ b/src/test/01-Basic/256-type-type.txt @@ -0,0 +1,144 @@ +-------------------------------------------------- +type name using .name(): int[][] +type name using .baseName(): int[][] +inherits: @array -> Object +isa...Object:1 UGen:0 UAna:0 +primitive: NO +array: YES depth: 2 +-------------------------------------------------- +type name using .name(): int[][] +type name using .baseName(): int[][] +inherits: @array -> Object +isa...Object:1 UGen:0 UAna:0 +primitive: NO +array: YES depth: 2 +-------------------------------------------------- +type name using .name(): SinOsc +type name using .baseName(): SinOsc +inherits: Osc -> UGen -> Object +isa...Object:1 UGen:1 UAna:0 +primitive: NO +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): SinOsc +type name using .baseName(): SinOsc +inherits: Osc -> UGen -> Object +isa...Object:1 UGen:1 UAna:0 +primitive: NO +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): int +type name using .baseName(): int +inherits: (nothing) +isa...Object:0 UGen:0 UAna:0 +primitive: YES +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): float +type name using .baseName(): float +inherits: (nothing) +isa...Object:0 UGen:0 UAna:0 +primitive: YES +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): time +type name using .baseName(): time +inherits: (nothing) +isa...Object:0 UGen:0 UAna:0 +primitive: YES +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): dur +type name using .baseName(): dur +inherits: (nothing) +isa...Object:0 UGen:0 UAna:0 +primitive: YES +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): complex +type name using .baseName(): complex +inherits: (nothing) +isa...Object:0 UGen:0 UAna:0 +primitive: YES +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): polar +type name using .baseName(): polar +inherits: (nothing) +isa...Object:0 UGen:0 UAna:0 +primitive: YES +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): vec3 +type name using .baseName(): vec3 +inherits: (nothing) +isa...Object:0 UGen:0 UAna:0 +primitive: YES +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): vec4 +type name using .baseName(): vec4 +inherits: (nothing) +isa...Object:0 UGen:0 UAna:0 +primitive: YES +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): DAC +type name using .baseName(): DAC +inherits: UGen_Stereo -> UGen_Multi -> UGen -> Object +isa...Object:1 UGen:1 UAna:0 +primitive: NO +array: NO depth: 0 +-------------------------------------------------- +type name using .name(): MFCC +type name using .baseName(): MFCC +inherits: UAna -> UGen -> Object +isa...Object:1 UGen:1 UAna:1 +primitive: NO +array: NO depth: 0 +StkInstrument: BandedWG +StkInstrument: BeeThree +StkInstrument: BlowBotl +StkInstrument: BlowHole +StkInstrument: Bowed +StkInstrument: Brass +StkInstrument: Clarinet +StkInstrument: Flute +StkInstrument: FM +StkInstrument: FMVoices +StkInstrument: FrencHrn +StkInstrument: HevyMetl +StkInstrument: HnkyTonk +StkInstrument: KrstlChr +StkInstrument: Mandolin +StkInstrument: ModalBar +StkInstrument: Moog +StkInstrument: PercFlut +StkInstrument: Rhodey +StkInstrument: Saxofony +StkInstrument: Shakers +StkInstrument: Sitar +StkInstrument: StifKarp +StkInstrument: TubeBell +StkInstrument: VoicForm +StkInstrument: Wurley +UAna: AutoCorr +UAna: Centroid +UAna: Chroma +UAna: DCT +UAna: FeatureCollector +UAna: FFT +UAna: Flip +UAna: Flux +UAna: IDCT +UAna: IFFT +UAna: Kurtosis +UAna: MFCC +UAna: RMS +UAna: RollOff +UAna: SFM +UAna: UnFlip +UAna: XCorr +UAna: ZeroX +SinOsc UGen +SinOsc SinOsc